在 ghci 中執行以下代碼:
import qualified Data.Map.Strict as Map
f = Map.fromList . zip
將給出以下錯誤:
<interactive>:16:20: error:
? Couldn't match type ‘[b0] -> [(a, b0)]’ with ‘[(k, a1)]’
Expected type: [a] -> [(k, a1)]
Actual type: [a] -> [b0] -> [(a, b0)]
? Probable cause: ‘zip’ is applied to too few arguments
In the second argument of ‘(.)’, namely ‘zip’
In the expression: Map.fromList . zip
In an equation for ‘f’: f = Map.fromList . zip
? Relevant bindings include
f :: [a] -> Map.Map k a1 (bound at <interactive>:16:1)
我本來希望 f 是型別的函式發生了[a] -> [b] -> Map a b
什么?
uj5u.com熱心網友回復:
如果您zip使用另一個函式進行組合,則意味著您正在將其視為一個函式
郵編 :: [a] -> ( [b] -> [(a,b)] )
所以你組合它的函式需要有一個 type 的引數[b] -> [(a,b)]。但是 的論證Map.fromList只是[(a,b)]一部分,即它要求另一個論證也已經被應用。
有幾種方法可以解決這個問題:
以非咖喱形式使用該函式。這具有您在這里似乎期望的行為 - 即
Map.fromList . uncurry zip型別檢查 - 但是這意味著整個事情也將采用元組形式的串列引數,這有點被 Haskellers 鄙視f :: Ord a => ([a], [b]) -> Map.Map a b f = Map.fromList . uncurry zip當然,你可以“撤銷 uncurrying”
f :: Ord a => [a] -> [b] -> Map.Map a b f = curry $ Map.fromList . uncurry zip但這有點傻。
fromList在另一個論點之后用它自己的版本進行組合。這可以作為組合運算子的運算子部分來完成:f = (Map.fromList . ) . zip同樣的事情也可以通過點擊
Functor (c->)實體來實作f = fmap Map.fromList . zip我不是這兩個的粉絲。
只需使至少一個論點有意義。
f keys = Map.fromList . zip keys這就是我推薦的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440927.html
標籤:哈斯克尔
