简体   繁体   English

Haskell文件夹在列表中

[英]Haskell foldr on list

I have a member function in Haskell for a BST. 我在Haskell的BST中有一个成员函数。 I am trying to use foldr to check if every element in the list is in the BST. 我正在尝试使用文件夹检查列表中的每个元素是否在BST中。 My member functions works for single elements and the first line looks like this. 我的成员函数适用于单个元素,第一行看起来像这样。

member ::(Ord a) =>  BST a -> a -> Bool

and when I have: 当我有:

let val = foldr mBST my_list 

I get the following error: 我收到以下错误:

Couldn't match expected type `a0 -> b0 -> b0'
            with actual type `BST Int'
In the first argument of `foldr', namely `mBST'
In the expression: foldr mBST my_list

I know foldr has type foldr :: (a -> b -> b) -> b -> [a] -> b, so my types are not matching but I don't know how to fix it or if it even possible to use foldr in this situation. 我知道foldr的类型为folder ::(a-> b-> b)-> b-> [a]-> b,所以我的类型不匹配,但我不知道如何解决它,甚至可能在这种情况下使用文件夹。 But I need to check to see if every element in the list is in my BST. 但是我需要检查列表中的每个元素是否都在我的BST中。

foldr recieve 3 arguments. foldr接收3个参数。 A binary function (operator), the accumulator (neutral operand of the operator) and the structure (List). 二进制函数(运算符),累加器(运算符的中性操作数)和结构(列表)。

And you call function could be: 您调用的函数可能是:

foldr (&&) true $ map (member mBST) myList

:t member mBST
 member mBST :: (Ord a) => a -> Bool

map function aplied an unary operator (or a function that recieve one argument) on the elements of the array. map函数在数组的元素上添加了一元运算符(或接受一个参数的函数)。 After you aplied member with mBST on every member is on your structure (a list) then check if all are true using foldr . 当你应用。查阅全文membermBST的每个成员都在你的结构(列表),然后检查是否都是true使用foldr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM