gpt4 book ai didi

映射中的 Haskell 错误

转载 作者:行者123 更新时间:2023-12-01 01:41:49 26 4
gpt4 key购买 nike

我是 haskell 新手,但我正在研究一个名为 mfilter 的函数,如果元素落入传入范围,它将从列表中排除元素,如下所示:

mfilter [(3,7)] [1..10] = [1,2,8,9,10]
mfilter [(10,18), (2,5), (20,20)] [1..25] = [1,6,7,8,9,19,21,22,23,24,25]
mfilter [('0','9')] "Sat Feb 8 20:34:50 2014" = "Sat Feb :: "

在获取范围时,我试图编写一个辅助函数来从这些范围中排除数字,但我遇到了太多打字问题,以至于我不知道从哪里开始。这是我的代码:

mfilter :: Ord a => [(a, a)] -> [a] -> [a]
mfilter (range:t) list = mfilter t (map (exclude range) list)

exclude :: Ord a => (a, a) -> [a] -> [a]
exclude _ [] = []
exclude (first, last) (x:t)
| x < first && x > last = x : map (exclude (first, last)) t
| otherwise = map (exclude (first, last)) t

这是我的错误:

Prelude> :l mfilter.hs
[1 of 1] Compiling Main ( mfilter.hs, interpreted )

mfilter.hs:5:42:
Could not deduce (a ~ [a])
from the context (Ord a)
bound by the type signature for
mfilter :: Ord a => [(a, a)] -> [a] -> [a]
at mfilter.hs:4:12-42
`a' is a rigid type variable bound by
the type signature for mfilter :: Ord a => [(a, a)] -> [a] -> [a]
at mfilter.hs:4:12
Expected type: [a] -> a
Actual type: [a] -> [a]
In the return type of a call of `exclude'
In the first argument of `map', namely `(exclude range)'
In the second argument of `mfilter', namely
`(map (exclude range) list)'

mfilter.hs:5:57:
Could not deduce (a ~ [a])
from the context (Ord a)
bound by the type signature for
mfilter :: Ord a => [(a, a)] -> [a] -> [a]
at mfilter.hs:4:12-42
`a' is a rigid type variable bound by
the type signature for mfilter :: Ord a => [(a, a)] -> [a] -> [a]
at mfilter.hs:4:12
Expected type: [[a]]
Actual type: [a]
In the second argument of `map', namely `list'
In the second argument of `mfilter', namely
`(map (exclude range) list)'
In the expression: mfilter t (map (exclude range) list)

mfilter.hs:11:44:
Could not deduce (a ~ [a])
from the context (Ord a)
bound by the type signature for
exclude :: Ord a => (a, a) -> [a] -> [a]
at mfilter.hs:8:12-40
`a' is a rigid type variable bound by
the type signature for exclude :: Ord a => (a, a) -> [a] -> [a]
at mfilter.hs:8:12
Expected type: [a] -> a
Actual type: [a] -> [a]
In the return type of a call of `exclude'
In the first argument of `map', namely `(exclude (first, last))'
In the second argument of `(:)', namely
`map (exclude (first, last)) t'

mfilter.hs:11:67:
Could not deduce (a ~ [a])
from the context (Ord a)
bound by the type signature for
exclude :: Ord a => (a, a) -> [a] -> [a]
at mfilter.hs:8:12-40
`a' is a rigid type variable bound by
the type signature for exclude :: Ord a => (a, a) -> [a] -> [a]
at mfilter.hs:8:12
Expected type: [[a]]
Actual type: [a]
In the second argument of `map', namely `t'
In the second argument of `(:)', namely
`map (exclude (first, last)) t'
In the expression: x : map (exclude (first, last)) t

(以及更多)我知道它看起来很多,但这些东西似乎是相关的,我无法终生弄清楚 haskell 试图用 Could not deduce (a ~[一])从上下文 (Ord a)... 对初学者有什么建议吗?

最佳答案

map的第一个参数是a -> b,不是[a] -> [a]。因此,如果要使用map,exclude的类型应该是(a, a) -> a -> b

但是,我不明白您为什么首先要使用 map 。 map 投影列表,而不是过滤它。 map 的结果始终是一个与原始列表长度相同的列表。永远不会有不同的长度。

如果你想过滤列表,你应该使用filter函数。它接受一个谓词和一个列表并返回过滤后的列表:

exclude (first, last) = filter (\x -> x >= first && x <= last)

同时,我可以看到您正在尝试使用尾递归来构建排除函数。如果那是您的目标,那么您也不应该使用 map 。只需从您的代码中删除所有提及 map 的地方,就可以开始了:

exclude (first, last) (x:t) =
| x < first && x > last = x : exclude (first, last) t
| otherwise = exclude (first, last) t

然而,使用递归并不是一个好主意(除非这是你的家庭作业)。它很容易出错,同时它已经在 fold 内(因此在 map 和 filter 内)为您很好地抽象了。

关于映射中的 Haskell 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21900843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com