不在范围内:数据构造函数简单代码 [英] Not in scope: data constructor simple code

查看:83
本文介绍了不在范围内:数据构造函数简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在haskell中的第一个程序无法编译:

My first program in haskell doesn't compile:

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:1:1: Not in scope: data constructor `RemoveOdd'

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

my.hs:6:37: Not in scope: data constructor `RemoveOdd'

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

my.hs:7:22: Not in scope: data constructor `RemoveOdd'

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

my.hs:9:6: Not in scope: data constructor `RemoveOdd'

推荐答案

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

Functions may not start with an uppercase letter. Only types, type constructors, classes, modules or data constructors may start with an uppercase letter:

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]

话虽如此,您可能应该看看模式匹配和filter,因为removeOdd = filter even.

That being said, you should probably have a look at pattern matching, and filter, since removeOdd = filter even.

这篇关于不在范围内:数据构造函数简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆