bluewolfxD
bluewolfxD

Reputation: 69

Haskell trying to implement null function on if

So basically i have a question for ym homework that says: "Create a function that checks if a list is not empty using prelude functions" I saw the null function and i wanted to use it and print the message. So i tried the following:

notEmpty :: [Int] -> [Char]
notEmpty [x] = if (null[x]) then "False" else "True"

If i call notEmpty [ ], it gives me this error: Non.Exhaustive patterns in function notEmpty after several learning i came up with this:

notEmpty :: [Int] -> [Char]
notEmpty [] = "False"
notEmpty [x] = if (null[x])then "False" else "True"

but after this, i tried the following input: notEmpty[1,2] and it gave me the same error.

My question is, when i run null [1,2] it gives me False, so what am i doing wrong?

Upvotes: 0

Views: 141

Answers (1)

[x] doesn't mean "a list called x". It means "a list with one element, where the one element is called x". Just use x instead of [x], and your first way will work:

notEmpty :: [Int] -> [Char]
notEmpty x = if (null x) then "False" else "True"

Upvotes: 9

Related Questions