Version française
Home     About     Download     Resources     Contact us    
Browse thread
List comprehensions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] List comprehensions
On Saturday 18 August 2007 15:44:04 Joel wrote:
> On Aug 18, 2007, at 3:28 PM, Jon Harrop wrote:
> > # [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
> > - : int list = [3; 7; 10; 14]
>
> How do you do something like this?
>
> [x + 2*x + y/2 | x <- [1; 2; 3; 4], [y <- [3; 4; 5]];;
>
> That is a list comprehension with more than one variable where all
> variables come from lists.

Ooh, look:

# let rec sort = function
    | [] -> []
    | x::xs -> sort [y | y <- xs; y<x] @ [x] @ sort [y | y <- xs; y>=x];;
val sort : 'a list -> 'a list = <fun>
# sort [3;1;6;2;7;4;9;8];;
- : int list = [1; 2; 3; 4; 6; 7; 8; 9]

Hmm, maybe we could form some sort of rudimentary lathe...

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e