简体   繁体   中英

Haskell foldr on list

I have a member function in Haskell for a BST. I am trying to use foldr to check if every element in the list is in the 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. But I need to check to see if every element in the list is in my BST.

foldr recieve 3 arguments. 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. After you aplied member with mBST on every member is on your structure (a list) then check if all are true using foldr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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