Haskell也许是类型构造函数

时间:2016-04-09 03:49:13

标签: haskell

我正在尝试在将整数传递给函数并获取Maybe类型的上下文中,对Maybe a类型构造函数进行非常简单的实现。

我正在研究的具体情况是一个函数,它接受一个整数并返回该数字是否在某个范围内。

data Maybe Number = Nothing | Just FirstRange | Just SecondRange | Just ThirdRange | Just FourthRange | Just FifthRange
   deriving(Show)
checknum :: Integer -> Maybe Number
checknum numinput
    |numinput >=0 && < 50 = Just FirstRange
    |numinput >=50 && < 60 = Just SecondRange
    |numinput >=60 && < 70 = Just ThirdRange
    |numinput >=70 && < 80 = Just FourthRange
    |numinput >=80 && < 100 = Just FifthRange
    |numinput < 0 || numinput > 100 = Nothing

然而,这个实现只是给我一个错误:

Unexpected type 'Number' in the data declaration for 'Maybe'
A data declaration should have form
 data Maybe a = ...

1 个答案:

答案 0 :(得分:5)

无需定义Maybe,只需定义Number类型,然后替换Maybe a中的“a”:

data Number = FirstRange | SecondRange | ThirdRange | FourthRange | FifthRange

此外,您缺少一些numinput <