I am very beginner with Haskell, and I m trying to create some small algorithms to understand the syntax of Haskell, I m trying to write and compile a quicksort algorithm, but it gives me an error at the compilation.
quicksort [] = []
quicksort (x:xs) = quicksort small (x : quicksort large)
where small = [y | y <- xs, y <= x]
large = [y | y <- xs, y > x]
Can someone help me to understand why I get this error at the 2nd line of my code.
parse error on input ‘=’parser
.
Thanks,
I m also new at this platform so if I m not clear enough or I need to improve my question let me know at the comments.
CodePudding user response:
Intendation matters in haskell. I can reproduce you error with
quicksort [] = []
quicksort (x:xs) = quicksort small (x : quicksort large)
where small = [y | y <- xs, y <= x]
large = [y | y <- xs, y > x]
.code.tio.hs:2:19: error: parse error on input ‘=’ Perhaps you need a 'let' in a 'do' block?
But when you remove the space before the second quicksort
, it compiles fine.
Technically, you can use any intendation when you use {
, }
and ;
. This is not recommended, because you will have a difficult time finding the exact places where you need braces, and examples are rare. ("Rare" as in "endangered species" / "almost extinct")
{
quicksort [] = [];
quicksort (x:xs) = quicksort small (x : quicksort large) -- no `;` before `where`
where {small = [y | y <- xs, y <= x];
large = [y | y <- xs, y > x]};
main = print . quicksort $ "hello"
}