[英]Trying to understand indentations in haskell
我正在努力制作一個解決河內塔的計划。 我為鍛煉做了一點復雜:
hanoi :: [Int] -> String
hanoi n
| n > 0 = hanoi' n "1" "2" "3"
where hanoi' n a b c
| n == 0 = "|"
| otherwise = let pre = hanoi' (n-1) a c b
posle = hanoi' (n-1) b c a
in pre ++ a ++ " ~~> " ++ c ++ posle
| otherwise = "Number must be greater than 0!!!"
但我得到:
hanoi.hs:7:97: parse error on input ‘=’
有人可以向我解釋發生了什么事嗎? 我現在看到我不理解該語言的一個重要部分。
你where
有來所有后衛后 。
hanoi :: [Int] -> String
hanoi n
| n > 0 = hanoi' n "1" "2" "3"
| otherwise = "Number must be greater than 0!!!"
where hanoi' n a b c
| n == 0 = "|"
| otherwise = let pre = hanoi' (n-1) a c b
posle = hanoi' (n-1) b c a
in pre ++ a ++ " ~~> " ++ c ++ posle
這在4.4.3中說明 ,雖然如果你不習慣類似BNF的語法,它有點隱藏:
decl → (funlhs | pat) rhs
funlhs → var apat { apat }
| pat varop pat
| ( funlhs ) apat { apat }
rhs → = exp [where decls]
| gdrhs [where decls]
gdrhs → guards = exp [gdrhs]
guards → | guard1, …, guardn (n ≥ 1)
guard → pat <- infixexp (pattern guard)
| let decls (local declaration)
| infixexp (boolean guard)
rhs
是重要的象征。 它是唯一包含可選where
。 guard
只能包含表達,其后是其他警衛。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.