不在范围内:数据构造函数简单代码

时间:2015-07-14 06:15:47

标签: haskell

我在haskell的第一个程序没有编译:

RemoveOdd nums =
    if null nums
    then []
    else 
        if mod (head nums) 2 == 0
        then (head nums) : (RemoveOdd (tail nums))
        else RemoveOdd (tail nums)

main=RemoveOdd [1,2,3,4,5,6,7,8]        

my.hs:1:1:不在范围内:数据构造函数`RemoveOdd'

my.hs:6:37:不在范围内:数据构造函数`RemoveOdd'

my.hs:7:22:不在范围内:数据构造函数`RemoveOdd'

my.hs:9:6:不在范围内:数据构造函数`RemoveOdd'

1 个答案:

答案 0 :(得分:3)

函数不能以大写字母开头。只有类型,类型构造函数,类,模块或数据构造函数可以以大写字母开头:

removeOdd nums =
    if null nums
    then []
    else 
        if mod (head nums) 2 == 0
        then (head nums) : (removeOdd (tail nums))
        else removeOdd (tail nums)

main = removeOdd [1,2,3,4,5,6,7,8]

话虽这么说,你应该看看模式匹配,filterremoveOdd = filter even