--To check the list is in ascending order or not
ascending::[Int]->Bool
ascending [] =True
ascending ((length l) == 1) =True
ascending l =((head l)<=l !! 1) && ascending(tail l)
This is my Haskell code and when I try to run this code in my GHCI interpreter I get the following error:
ascending.hs(File_name):4:13: Parse error in pattern: length
Failed, modules loaded: none.
Could anyone tell me where the bug is?
It looks like you're trying to add a guard. You need to bind the input list to l
and separate the condition with |
:
ascending :: [Int]->Bool
ascending [] =True
ascending l | ((length l) == 1) =True
ascending l = ((head l)<=l !! 1) && ascending(tail l)
You can replace your use of guards, head
and tail
with pattern matching instead:
ascending :: [Int]->Bool
ascending [] = True
ascending [_] = True
ascending (x:y:xs) = (x <= y) && ascending (y:xs)