0
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerOrEqual = [a | a <- xs, a <= x]
        larger = [a | a <- xs, a > x]
    in quicksort smallerOrEqual ++ [x] += larger

main = do
    a = [ 5, 1, 9, 4, 6, 7, 3]
    print quicksort a

I got this error:

[1 of 1] Compiling Main             ( quicksort.hs, quicksort.o )

quicksort.hs:10:11: parse error on input `='

Really don't undrestand why. Double checked this code several times. Still confusing. Need help. thanks!

2 Answers 2

3

You forgot a let,

quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerOrEqual = [a | a <- xs, a <= x]
        larger = [a | a <- xs, a > x]
    in quicksort smallerOrEqual ++ [x] ++ quicksort larger -- ++ not +=

main = do
    let a = [ 5, 1, 9, 4, 6, 7, 3]
    print (quicksort a) -- The parens are needed too

I fixed a few other errors too.

It's worth noting that this quicksort isn't in place and therefore is not really quicksort, though it is pretty.

Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, this quickSort implementation is pretty much Haskell's signature code snippet since it's so pretty.
Shouldnt it be in (quicksort smallerOrEqual) ++ [x] ++ (quicksort larger) ?
@ssm Function application has the highest precedence, it doesn't matter
why is this not real quicksort? Not in place ? require more spaces,right? Other than that, this still implements quick sort algorithm IMO.
@ValekHalfHeart Nope, you just need to use the ST monad, this means you essentially are writing C with a nicer calling interface though :/
|
3

Within do blocks, bindings must be preceded by the let keyword, so the line should be let a = [ 5, 1, 9, 4, 6, 7, 3].

There are a few other errors, however. You appear to have mistyped the ++ operator and you forgot to sort the larger section of the list. Your print statement won't work because print quicksort a is parsed as passing both the function quickSort and the value a to print. You can easily fix this using parentheses or the function application operator ($). This code works perfectly:

quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerOrEqual = [a | a <- xs, a <= x]
        larger = [a | a <- xs, a > x]
    in quicksort smallerOrEqual ++ [x] ++ quickSort larger

main = do
    let a = [ 5, 1, 9, 4, 6, 7, 3]
    print $ quicksort a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.