我是一個Haskell初學者,當我編譯Haskell源代碼的時候,嘗試做一個懶惰的函式時至少只要
module Main where
import Data.Maybe
main = undefined
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
foldWhilel f acc xs = go acc xs
where
go :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
go acc [] = Just acc
go acc (x:xs) = if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc
考慮到(至少據我所知)這個錯誤令人討厭的愚蠢胡說八道錯誤,這應該完全有效。因為函式go和函式引數在型別注解中設計的很好
dist-newstyle/gh.hs:15:19: error:
? Couldn't match type ‘b2’ with ‘b’
‘b2’ is a rigid type variable bound by
the type signature for:
new_acc :: forall b2. Maybe b2
at dist-newstyle/gh.hs:14:9-26
‘b’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
Expected type: Maybe b2
Actual type: Maybe b
? In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
In an equation for ‘go’:
go acc (x : xs)
= if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc
? Relevant bindings include
new_acc :: Maybe b2 (bound at dist-newstyle/gh.hs:15:9)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 | new_acc = f x acc
| ^^^^^^^
dist-newstyle/gh.hs:15:21: error:
? Couldn't match expected type ‘a’ with actual type ‘a1’
‘a1’ is a rigid type variable bound by
the type signature for:
go :: forall b1 a1. b1 -> [a1] -> Maybe b1
at dist-newstyle/gh.hs:10:5-29
‘a’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
? In the first argument of ‘f’, namely ‘x’
In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
? Relevant bindings include
xs :: [a1] (bound at dist-newstyle/gh.hs:12:15)
x :: a1 (bound at dist-newstyle/gh.hs:12:13)
go :: b1 -> [a1] -> Maybe b1 (bound at dist-newstyle/gh.hs:11:5)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 | new_acc = f x acc
| ^
dist-newstyle/gh.hs:15:23: error:
? Couldn't match expected type ‘b’ with actual type ‘b1’
‘b1’ is a rigid type variable bound by
the type signature for:
go :: forall b1 a1. b1 -> [a1] -> Maybe b1
at dist-newstyle/gh.hs:10:5-29
‘b’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
? In the second argument of ‘f’, namely ‘acc’
In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
? Relevant bindings include
acc :: b1 (bound at dist-newstyle/gh.hs:12:8)
go :: b1 -> [a1] -> Maybe b1 (bound at dist-newstyle/gh.hs:11:5)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 | new_acc = f x acc
|
uj5u.com熱心網友回復:
修復錯誤的最簡單方法是洗掉go和的型別簽名new_acc。
您不能以這種方式使用型別簽名,因為b在簽名中使用foldWhilel和b在簽名中使用new_acc是兩個完全不同的變數,它們恰好具有相似的名稱。最重要的是,(a -> b -> Maybe b)簽名go是錯誤的(go沒有這樣的引數,是f從外部范圍使用)。
如果您想在b整個函式中使用相同的含義,則需要啟用并應用Scoped 型別變數:
{-# Language ScopedTypeVariables #-}
import Data.Maybe
foldWhilel :: forall a b. (a -> b -> Maybe b) -> b -> [a] -> Maybe b
foldWhilel f acc xs = go acc xs
where
go :: b -> [a] -> Maybe b
go acc [] = Just acc
go acc (x:xs) = if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/477820.html
標籤:哈斯克尔
