哈斯克爾表示其存在的問題和期望的錯誤信息,這樣
Prelude> :{
Prelude| let
Prelude| {
Prelude| rep' :: Int -> a -> [a];
Prelude| rep' 0 x = [];
Prelude| rep' n x = x:rep' (n-1, x);
Prelude| }
Prelude| :}
<interactive>:73:22:
Couldn't match expected type `[a]' with actual type `a0 -> [a0]'
In the return type of a call of rep'
Probable cause: rep' is applied to too few arguments
In the second argument of `(:)', namely `rep' (n - 1, x)'
In the expression: x : rep' (n - 1, x)
<interactive>:73:27:
Couldn't match expected type `Int' with actual type `(Int, a)'
In the first argument of rep', namely `(n - 1, x)'
In the second argument of `(:)', namely `rep' (n - 1, x)'
In the expression: x : rep' (n - 1, x)
在第一部分中,
Couldn't match expected type `[a]' with actual type `a0 -> [a0]'
In the return type of a call of rep'
Probable cause: rep' is applied to too few arguments
說,你已經宣佈rep'
返回類型爲[a]
,但它返回a0 -> [a0]
,這意味着它正在返回一個部分應用的函數。可能的問題也給你一個提示
Probable cause: rep' is applied to too few arguments
,所以你可能會傳遞較少函數的自變量rep'
。而在下一節,線路
Couldn't match expected type `Int' with actual type `(Int, a)'
表示,它期待一個Int
,但它得到了(Int, a)
。在Haskell中,當你說(n-1, x)
時,它被視爲一個元組對象,其中包含兩個元素。所以,你實際上用一個元組對象調用rep'
,而不是兩個參數。
實際調用rep'
有兩個參數,你可以像這樣
rep' n x = x:rep' (n-1) x
現在,你有兩個參數,(n-1)
和x
調用rep'
。
Prelude> :{
Prelude| let
Prelude| {
Prelude| rep' :: Int -> a -> [a];
Prelude| rep' 0 x = [];
Prelude| rep' n x = x:rep' (n-1) x;
Prelude| }
Prelude| :}
Prelude> rep' 5 100
[100,100,100,100,100]
Haskell函數被稱爲'fun arg1 arg2 ...''不是'fun(arg1,arg2,...)'。 – AJFarmar
@AJFarmar這些都是完全合法的,取決於「樂趣」的類型。重要的是你調用與你定義的相同類型的函數。 – sepp2k
@ sepp2k我意識到,但爲了學習Haskell,向初學者展示咖喱方法是理想的選擇。 – AJFarmar