我有一個像這樣((:) . ((:) x))傳遞的 lambda 函式:二維串列在哪里。我想重構以使其更清晰(這樣我可以更好地理解它)。我想它會這樣做:foldrfoldr ((:) . ((:) x)) [] xsxs
foldr (\ element acc -> (element:acc) . (x:acc)) [] xs
但這給了我錯誤:
ex.hs:20:84: error:
? Couldn't match expected type ‘a0 -> b0’ with actual type ‘[[a]]’
? Possible cause: ‘(:)’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘(x : acc)’
In the expression: (element : acc) . (x : acc)
In the first argument of ‘foldr’, namely
‘(\ element acc -> (element : acc) . (x : acc))’
? Relevant bindings include
acc :: [[a]] (bound at ex.hs:20:60)
element :: [a] (bound at ex.hs:20:52)
xs :: [[a]] (bound at ex.hs:20:30)
x :: [a] (bound at ex.hs:20:28)
prefixes :: [a] -> [[a]] (bound at ex.hs:20:1)
|
20 | prefixes = foldr (\x xs -> [x] : (foldr (\ element acc -> (element:acc) . (x:acc)) [] xs)) []
|
編輯:我圍繞這個片段的所有相關代碼是
prefixes :: Num a => [a] -> [[a]]
prefixes = foldr (\x acc -> [x] : (foldr ((:) . ((:) x)) [] acc)) []
它的呼叫是:
prefixes [1, 2, 3]
如何重構 lambda((:) . ((:) x))以包含它的兩個引數?
uj5u.com熱心網友回復:
您可以逐步將其轉換為 lambda。
(:) . ((:) x)
\y -> ((:) . (((:) x)) y -- conversion to lambda
\y -> (:) (((:) x) y) -- definition of (.)
\y -> (:) (x : y) -- rewrite second (:) using infix notation
\y z -> (:) (x : y) z -- add another parameter
\y z -> (x : y) : z -- rewrite first (:) using infix notation
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/446072.html
上一篇:AlpineJs:回圈中的回圈-如何在第二個回圈中使用第一個變數?
下一篇:型別級約束編碼
