在Haskell中,我嘗試這樣做:功能重複N次錯誤
multi::(Num n)=>n->((a->a)->(a->a))
(multi 0) f x=x
(multi n) f x=(multi n-1) f (f x)
希望能得到那會重複另一個函數的次數的函數。 在ghci中,我得到這個:
[1 of 1] Compiling Main (pad.hs, interpreted)
pad.hs:2:8:
Could not deduce (Eq n) arising from the literal `0'
from the context (Num n)
bound by the type signature for
multi :: Num n => n -> (a -> a) -> a -> a
at pad.hs:1:8-35
Possible fix:
add (Eq n) to the context of
the type signature for multi :: Num n => n -> (a -> a) -> a -> a
In the pattern: 0
In an equation for `multi': multi 0 f x = x
pad.hs:3:23:
Could not deduce (Num ((a -> a) -> a -> a))
arising from a use of `-'
from the context (Num n)
bound by the type signature for
multi :: Num n => n -> (a -> a) -> a -> a
at pad.hs:1:8-35
Possible fix:
add an instance declaration for (Num ((a -> a) -> a -> a))
In the expression: multi n - 1
In the expression: (multi n - 1) f (f x)
In an equation for `multi': multi n f x = (multi n - 1) f (f x)
Failed, modules loaded: none.
我哈斯克爾一個新手。我該怎麼辦?
非常好,簡潔。其他的問題都放在他們的代碼中,但你是唯一明確表示要做'(n-1)'而不是'n-1'的人。 – PyRulez