2015-05-09 163 views
-1

這裏是代碼:爲什麼這種遞歸的Haskell函數不起作用?

rep' :: Int -> a -> [a] 
rep' 0 x = [] 
rep' n x = x:rep'(n-1, x) 

我試圖把它改寫這樣的:

rep' :: Int -> a -> [a] 
rep' 0 x = [] 
rep' n x = x:(rep' n-1 x) 

,但它也不起作用。

baby.hs:3:15-20: Couldn't match expected type ‘[a]’ with actual type ‘a0 -> [a0]’ … 
    Relevant bindings include 
     x :: a (bound at /Users/hanfeisun/Workspace/haskell/baby.hs:3:8) 
     rep' :: Int -> a -> [a] 
     (bound at /Users/hanfeisun/Workspace/haskell/baby.hs:2:1) 
    Probable cause: ‘rep'’ is applied to too few arguments 
    In the first argument of ‘(-)’, namely ‘rep' n’ 
    In the second argument of ‘(:)’, namely ‘(rep' n - 1 x)’ 
Compilation failed. 
λ> 

有沒有人有這方面的想法?

+0

Haskell函數被稱爲'fun arg1 arg2 ...''不是'fun(arg1,arg2,...)'。 – AJFarmar

+0

@AJFarmar這些都是完全合法的,取決於「樂趣」的類型。重要的是你調用與你定義的相同類型的函數。 – sepp2k

+0

@ sepp2k我意識到,但爲了學習Haskell,向初學者展示咖喱方法是理想的選擇。 – AJFarmar

回答

10

哈斯克爾表示其存在的問題和期望的錯誤信息,這樣

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] 
5

rep'的第一個參數應該是Int,但是當您將其稱爲rep' (n-1, x)時,第一個也是唯一的參數是元組。

相關問題