arbipher
V2EX  ›  Haskell

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

  •  
  •   arbipher · Nov 27, 2014 · 6791 views
    This topic created in 4184 days ago, the information mentioned may be changed or developed.
    我想针对函数参数为空列表、一个元素、更多元素分别定义

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

    发现只有
    foo xs | length xs == 1 = 0
    这么定义才work,感觉很丑。。。
    1 replies    2014-11-27 14:37:48 +08:00
    arbipher
        1
    arbipher  
    OP
       Nov 27, 2014
    原来是函数体写错了。。。
    让它沉下去吧。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4241 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.