Leo Pkm
Leo Pkm

Reputation: 13

using guard inside where bindings in Haskell language

i am a newbie in haskell language and i would like to use guard inside where binding but i get error: Not in scope: `x' Failed, modules loaded: none.

describelist'' :: [a] ->String
describelist'' xs = "the list is : " ++ what xs
    where what xs
              | xs == "" = "empty list "
              | xs ==[x] = "singleton"
              | otherwise = "list more than or equal two elements"

i think that my code is right but error always appear,

Upvotes: 0

Views: 189

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

xs == [x] makes no sense as guard, since you did not define x, this is a pattern. The same for xs == "", since it is not said that xs is a String, it can be an empty list of Ints for example.

You thus should use pattern matching:

describelist'' :: [a] -> String
describelist'' xs = "the list is : " ++ what xs
    where what [] = "empty list"
          what [_] = "singleton"
          what _ = "list more than or equal two elements"

Upvotes: 2

Related Questions