How to make a type with restrictions

前端 未结 5 1898
我在风中等你
我在风中等你 2020-12-01 12:06

For example I want to make a type MyType of integer triples. But not just Cartesian product of three Integer, I want the type to represent all (x, y, z) such that x + y + z

5条回答
  •  死守一世寂寞
    2020-12-01 12:48

    Just elaborating on ivanm's answer:

    data MyType = MT {x :: Int, y :: Int, z :: Int } deriving Show
    
    createMyType :: Int -> Int -> Int -> Maybe MyType
    createMyType a b c
        | a + b + c == 5 = Just MT { x = a, y = b, z = c }
        | otherwise      = Nothing
    

提交回复
热议问题