简体   繁体   中英

Member Function in Haskell

Working on a small assignment for class, having a lot of trouble with Haskell. I am trying to make a recursive method for finding if an integer is part of a list or not. I know the gist, but am unable to get it working correctly with the haskell syntax. Check if the current list is empty, if so then False, then check if integer is equal to the head of the current list, if so, then True, then call member again with the same value you are searching for, and the tail of the list. What can I do to get this functioning properly.

Currently this is what I have:

member ::Int -> [Int] -> Bool
member x y
if y [] then False
else if x == head y then True
else member x tail y

I have also tried using

member :: (Eq x) => x -> [x] -> Bool

as the beginning line, and also a much simplier :

let member x y =  if null y then False
else if x == head y then True
else member x tail y

Any help would be appreciated.

with pattern matching you can write it more clearly

member :: (Eq a) => a -> [a] -> Bool
member x [] = False
member x (y:ys) | x==y = True
                | otherwise = member x ys
element _ [] = False
element e (x:xs) = e == x || e `element` xs
-- OR
element e xs = if xs == [] then False
               else if e == head xs then True
                    else e `element` tail xs
-- OR
element e xs = xs /= [] && (e == head xs || e `element` tail xs)
-- x `op` y = op x y

-- If you're feeling cheeky
element = elem

Your syntax appears very confused, but your logic makes sense, so here's a bucket list of things to remember:

  • Functions can be defined by multiple equations. Equations are checked top to bottom. That means using = .

  • Pattern matches are not equality tests. A pattern match breaks a value into its constituents if it matches and fails otherwise. An equality test x == y returns a Bool about the equality of x and y .

  • Pattern matching is used for flow control via...

    • a case statement, like

       case xs of { [] -> ... x:xs' -> ... } 
    • Multiple equations, like

       element _ [] = ... element e (x:xs) = ... 

      Note that you can ignore a value in a pattern with _ . With multiple equations of a function with multiple arguments, you're really pattern matching on all the arguments at once.

  • Bool s are used for flow control via if _ then _ else _ :

     if xs == [] then False else True 

    which is really just

     case x == y of { True -> False False -> True } 

    and Bool s can use the ordinary operators (&&) ( infixr 3 ) and (||) ( infixr 2 )

  • The difference is especially nefarious on lists. instance Eq a => Eq [a] , so in order to use == on lists, you need to know that the elements of the lists can be compared for equality, too. This is true even when you're just checking (== []) . [] == [] actually causes an error, because the compiler cannot tell what type the elements are. Here it doesn't matter, but if you say, eg nonEmpty xs = xs /= [] , you'll get nonEmpty :: Eq a => [a] -> Bool instead of nonEmpty :: [a] -> Bool , so nonEmpty [not] gives a type error when it should be True .

  • Function application has the highest precedence, and is left-associative:

    • element x xs reads as ((element x) xs)
    • element x tail xs reads as (((element x) tail) xs) , which doesn't make sense here
  • f $ x = fx , but it's infixr 0 , which means it basically reverses the rules and acts like a big set of parentheses around its right argument

    • element x $ tail xs reads as ((element x) (tail xs)) , which works
  • Infix functions always have lower precedence than prefix application:

    • x `element` tail xs means ((element x) (tail xs)) , too
  • let decls in expr is an expression . decls is only in scope inside expr , and the entire thing evaluates to whatever expr evaluates to. It makes no sense on the top level.

  • Haskell uses indentation to structure code, like Python. Reference

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