Project Inquiry

Implement listElem

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (elem)

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

test :: Bool
test = foldl (&&) True [ listElem 1 [] == False
                       , listElem 1 [1, 2] == True
                       , listElem 3 [1, 2] == False
                       , listElem 'a' [] == False
                       , listElem 'a' "ab" == True
                       , listElem 'c' "ab" == False
                       ]

-- TODO: This program doesn't compile. Define the missing function(s) incl. type signatures!
Solution
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (elem)

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

test :: Bool
test = foldl (&&) True [ listElem 1 [] == False
                       , listElem 1 [1, 2] == True
                       , listElem 3 [1, 2] == False
                       , listElem 'a' [] == False
                       , listElem 'a' "ab" == True
                       , listElem 'c' "ab" == False
                       ]

-- DONE
listElem :: (Eq a) => a -> [a] -> Bool
listElem x xs = foldl (\acc curr -> acc || curr == x) False xs