2013-08-30 51 views
3

我定義如下功能listToNumber:的Haskell - 映射函數置換

listToNumber = foldl1 (\acc xs -> acc*10 + xs) 

它正常工作時僅經一個數字列表中提供,例如:

listToNumber [1,2,3,4] = 1234 
map listToNumber [[1,2,3,4], [5,4,3,2]] = [1234,5432] 

然而,下面的返回錯誤信息:

map listToNumber permutations[1..3] 

有人可以解釋一下嗎?

P.S.的錯誤味精是如下:

Couldn't match expected type `[t1] -> t0' with actual type `[b0]' 
The function `map' is applied to three arguments, 
but its type `([b0] -> b0) -> [[b0]] -> [b0]' has only two 
In the expression: map listToNumber permutations [1 .. 3] 
In an equation for `it': 
    it = map listToNumber permutations [1 .. 3] 

回答

7

嘗試map listToNumber (permutations [1 .. 3])

在ghci中可以檢查一個函數的類型或與:t

> :t map 
> map :: (a -> b) -> [a] -> [b] 

IM裝置map表達需要一個函數和一列表並返回一個列表,但在map listToNumber permutations [1 .. 3]中,您嘗試傳遞兩個函數和一個列表以映射 (因爲函數應用程序與左側關聯)。

0

另一變體:map listToNumber $ permutations [1..3]