gpt4 book ai didi

Haskell 错误 : Variable not in scope

转载 作者:行者123 更新时间:2023-12-04 03:13:03 25 4
gpt4 key购买 nike

我对 Haskell 有点陌生,但我已经解决了这个问题几个小时,但没有运气。

我正在尝试实现类似于过滤器的东西,除了谓词和列表被传递给一个函数,它返回两个列表的元组,一个由谓词过滤,一个不是。

divideList :: (a -> Bool) -> [a] -> ([a],[a])
divideList p xs = (f, nf) where
f = doFilter p xs
nf = doFilter (not . p) xs

doFilter :: (a -> Bool) -> [a] -> [a]
doFilter _ [] = []
doFilter p (x:xs) = [x | p x] ++ doFilter p xs

第二个函数, doFilter工作正常。它将过滤器应用于其列表并吐出适当的列表。 (即如果我只使用 doFilter (>3) [1,2,3,4,5,6] 它将正常工作)

我的问题是第一个功能。当我使用 divideList (>3) [1,2,3,4,5,6]我得到一个 Variable not in scope的号码错误。下面列出了错误:
AddListOperations.hs:20:23: error:
Variable not in scope: p :: a -> Bool

AddListOperations.hs:20:25: error: Variable not in scope: xs :: [a]

AddListOperations.hs:21:31: error:
Variable not in scope: p :: a -> Bool

AddListOperations.hs:21:34: error: Variable not in scope: xs :: [a]

就像我说的,我只在 Haskell 上玩了几天,所以如果我遗漏了任何重要信息,请告诉我。

最佳答案

两者都缩进 fnf :

divideList :: (a -> Bool) -> [a] -> ([a],[a])
divideList p xs = (f, nf) where
f = doFilter p xs
nf = doFilter (not . p) xs

毕竟,您的 where 在哪里?挡住?

顺便说一句, divideListpartition来自 Data.List .

关于Haskell 错误 : Variable not in scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429069/

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