V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
arbipher
V2EX  ›  Haskell

Haskell 怎么在只有一个元素的列表上定义函数

  •  
  •   arbipher · 2014-11-27 14:26:01 +08:00 · 5765 次点击
    这是一个创建于 3410 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想针对函数参数为空列表、一个元素、更多元素分别定义

    foo (x:[]) = 0
    foo (x:xs) | null xs = 0
    这两次能通过编译,但是不能一运行就报错。

    发现只有
    foo xs | length xs == 1 = 0
    这么定义才work,感觉很丑。。。
    1 条回复    2014-11-27 14:37:48 +08:00
    arbipher
        1
    arbipher  
    OP
       2014-11-27 14:37:48 +08:00
    原来是函数体写错了。。。
    让它沉下去吧。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2878 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:35 · PVG 19:35 · LAX 04:35 · JFK 07:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.