I am trying to implement the quicksort algorithm in OCaml, and I thought I had it, but it won’t compile, and I just can’t see what is wrong with it. Here’s my code:
let rec quicksort list =
match list with
[] -> []
|h::t -> append((quicksort (filter (largerthan h)
t))(quicksort(filter (smallerthan h) t)));;
let largerthan x y =
x<y;;
let smallerthan x y =
x>y;;
let rec append x y =
match x with
[] -> y
| h::t -> h:: append t y;;
let rec filter f list =
match list with
[] -> []
|h::t -> (if f h = true then h:: filter f t else filter f t);;
Now, when I try to use this in OCaml, it says “Error: This expression has type ‘a -> ‘b
but an expression was expected of type ‘a” while pointing to the last line of my quicksort function.
Does anybody know what’s going wrong??
Thanks a lot!
Linus
Edit: Okay, I’ve gotten rid of the original error (thanks to ADEpt :)). However, now the function just outputs an empty list regardless of the input… Does anybody know what’s going on there??
You have extra parens in the “apply” call. Instead of:
Write this: