tags:

views:

397

answers:

5

I have a simple f# quick sort function defined as:

let rec qsort(xs:List<int>) =

let smaller = xs |> List.filter(fun e -> e < xs.Head)
let larger = xs |> List.filter(fun e -> e > xs.Head)
match xs with
| [] -> []
| _ -> qsort(smaller)@[xs.Head]@qsort(larger)

Is there a way in f# to write it more like Haskell:

qsort       :: [Int] -> [Int]
qsort []     = []
qsort (x:xs) =
qsort smaller ++ [x] ++ qsort larger
where
  smaller = [a | a <- xs, a <= x]
  larger  = [b | b <- xs, b >= x]

I know the f# algorithm is missing a <= and >=. The question is more about syntax/readibility.

Thanks.

+6  A: 

You want your second match clause to be x :: xs, and to use the @ (append) operator where your Haskell example uses ++:

let rec qsort(xs : List<int>) =
  match xs with
  | [] -> []
  | x :: xs ->
      let smaller = qsort (xs |> List.filter(fun e -> e <= x))
      let larger = qsort (xs |> List.filter(fun e -> e >= x))
      smaller @ [x] @ larger

It's not quite the same as the Haskell definition by cases syntax, but hopefully similar enough for you!

itowlson
Minor note: I think `smaller @ x::larger`, rather than `smaller @ x @ larger`, is less pretty but a little faster in practice since it avoid re-copying your target list.
Juliet
"e >= x" should be "e > x" otherwise you will end up duplicating elements.
RodYan
+2  A: 

haskell 'where' syntax, which lets you use the name of a function before its definition, kind of maps to f# 'let rec ... and'

let qsort xs =
    let rec sort xs = 
        match ls with
        |[] -> ....
        |h::t -> (smaller t) @ h @ (larger t)

    and smaller ls =    //the 'and' lets you define the 
                        //  function after where it is used, 
                        //  like with 'where' in haskell
         ... define smaller in terms of sort
    and larger ls =
         ... same

    sort xs
dan
+5  A: 

This is the most 'Haskellian' way I can think of, the only thing missing is being able to declare smaller/larger as a 'where' clause:

let rec qsort:int list -> int list = function
    | [] -> []
    | x::xs -> let smaller = [for a in xs do if a<=x then yield a]
               let larger =  [for b in xs do if b>x then yield b]
               qsort smaller @ [x] @ qsort larger

I know it's not part of your question, but I'd use List.partition to split the list in smaller/larger in a single pass:

let rec qsort = function
    | [] -> []
    | x::xs -> let smaller,larger = List.partition (fun y -> y<=x) xs
               qsort smaller @ [x] @ qsort larger
cfern
+4  A: 

This seems to be as concise as it can get (combining the ideas from other answers, and using currying for operators):

let rec qsort = function
| [] -> []
| (x:int) :: xs ->
    let smaller = List.filter ((<=) x) xs
    let larger  = List.filter ((>=) x) xs
    qsort smaller @ [x] @ qsort larger
Pavel Minaev
+1  A: 

...Or you could make a tail recursive qsort by using CPS:

let qSort lst = 
   let rec qs l cont = 
       match l with
       | []      -> cont []
       | (x::xs) -> qs (List.filter (fun e -> e <= x) xs) (fun smaller ->
                    qs (List.filter (fun e -> e >  x) xs) (fun larger  ->
                        smaller @ (x :: larger) |> cont))
   qs lst id
primodemus