Reputation: 13
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
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 Int
s 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