你有什麼是兩個聲明,其中第二陰影第一。
您需要聲明len
作爲一個具有兩個子句的函數。在GHCI,你可以做這樣的:
:{
let len [] = 0
len (h:t) = 1 + len t
:}
的:{ ... :}
形式讓你輸入多行的聲明,你會在*.hs
文件。
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let len [] = 0
Prelude> let len (h:t) = 1 + len t -- this shadows the earlier len
Prelude> len [1, 2, 3]
*** Exception: <interactive>:3:5-25: Non-exhaustive patterns in function len
-- exception because the new len doesn't handle an empty list
Prelude> :{
Prelude| let len [] = 0
Prelude| len (h:t) = 1 + len t
Prelude| :}
Prelude> len [1, 2, 3]
3
Prelude>