caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* List comprehensions
@ 2007-08-18 14:28 Jon Harrop
  2007-08-18 14:44 ` [Caml-list] " Joel Reymont
  2007-08-18 14:54 ` skaller
  0 siblings, 2 replies; 7+ messages in thread
From: Jon Harrop @ 2007-08-18 14:28 UTC (permalink / raw)
  To: caml-list


I didn't know OCaml had list comprehensions built-in:

$ ocaml camlp4oof.cma
        Objective Caml version 3.10.0

        Camlp4 Parsing version 3.10.0

# [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
- : int list = [3; 7; 10; 14]
#

Cool! :-)

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-18 14:28 List comprehensions Jon Harrop
@ 2007-08-18 14:44 ` Joel Reymont
  2007-08-18 14:46   ` Jon Harrop
  2007-08-19 10:26   ` Nicolas Pouillard
  2007-08-18 14:54 ` skaller
  1 sibling, 2 replies; 7+ messages in thread
From: Joel Reymont @ 2007-08-18 14:44 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list


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.

	Thanks, Joel

--
http://wagerlabs.com






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-18 14:44 ` [Caml-list] " Joel Reymont
@ 2007-08-18 14:46   ` Jon Harrop
  2007-08-19 10:26   ` Nicolas Pouillard
  1 sibling, 0 replies; 7+ messages in thread
From: Jon Harrop @ 2007-08-18 14:46 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-18 14:28 List comprehensions Jon Harrop
  2007-08-18 14:44 ` [Caml-list] " Joel Reymont
@ 2007-08-18 14:54 ` skaller
  2007-08-20 22:55   ` Nathaniel Gray
  1 sibling, 1 reply; 7+ messages in thread
From: skaller @ 2007-08-18 14:54 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Sat, 2007-08-18 at 15:28 +0100, Jon Harrop wrote:
> I didn't know OCaml had list comprehensions built-in:
> 
> $ ocaml camlp4oof.cma
>         Objective Caml version 3.10.0
> 
>         Camlp4 Parsing version 3.10.0
> 
> # [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
> - : int list = [3; 7; 10; 14]
> #
> 
> Cool! :-)

Since you're fishing, I'm biting -- what bait do you
need to catch a camlp4oof?


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-18 14:44 ` [Caml-list] " Joel Reymont
  2007-08-18 14:46   ` Jon Harrop
@ 2007-08-19 10:26   ` Nicolas Pouillard
  1 sibling, 0 replies; 7+ messages in thread
From: Nicolas Pouillard @ 2007-08-19 10:26 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Jon Harrop, O'Caml Mailing List

Excerpts from Joel Reymont's message of Sat Aug 18 16:44:04 +0200 2007:
> 
> 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.
> 

Use `;' to separate generators. And you have an extra `['.

[x + 2*x + y/2 | x <- [1; 2; 3; 4]; y <- [3; 4; 5]];;

-- 
Nicolas Pouillard aka Ertai


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-18 14:54 ` skaller
@ 2007-08-20 22:55   ` Nathaniel Gray
  2007-08-22  8:54     ` Adrien
  0 siblings, 1 reply; 7+ messages in thread
From: Nathaniel Gray @ 2007-08-20 22:55 UTC (permalink / raw)
  To: skaller; +Cc: Jon Harrop, caml-list

On 8/18/07, skaller <skaller@users.sourceforge.net> wrote:
> On Sat, 2007-08-18 at 15:28 +0100, Jon Harrop wrote:
> > I didn't know OCaml had list comprehensions built-in:
> >
> > $ ocaml camlp4oof.cma
> >         Objective Caml version 3.10.0
> >
> >         Camlp4 Parsing version 3.10.0
> >
> > # [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
> > - : int list = [3; 7; 10; 14]
> > #
> >
> > Cool! :-)
>
> Since you're fishing, I'm biting -- what bait do you
> need to catch a camlp4oof?

I guess just 0.3.10.

[n8gray@golux]% locate camlp4oof
/usr/local/godi/bin/camlp4oof
/usr/local/godi/bin/camlp4oof.opt
/usr/local/godi/lib/ocaml/std-lib/camlp4/camlp4oof.cma

[n8gray@golux]% rlwrap ocaml camlp4oof.cma
        Objective Caml version 3.10.0

        Camlp4 Parsing version 3.10.0

# [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
- : int list = [3; 7; 10; 14]
# [x + 2*x + x/2 - y | x,y <- [1,2; 2,3; 3,4; 4,5]];;
- : int list = [1; 4; 6; 9]

-- 
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] List comprehensions
  2007-08-20 22:55   ` Nathaniel Gray
@ 2007-08-22  8:54     ` Adrien
  0 siblings, 0 replies; 7+ messages in thread
From: Adrien @ 2007-08-22  8:54 UTC (permalink / raw)
  To: Nathaniel Gray; +Cc: skaller, caml-list

[-- Attachment #1: Type: text/plain, Size: 316 bytes --]

I just tried this to avoid a big block of ugly code but it doesn't work with
3.09:

# [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
Characters 15-16:
  [x + 2*x + x/2 | x <- [1; 2; 3; 4]];;
                       ^
Syntax error


So it's a very recent addition, no surprise you didn't know had this. ;)



---

Adrien Nader

[-- Attachment #2: Type: text/html, Size: 618 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-08-22  8:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-18 14:28 List comprehensions Jon Harrop
2007-08-18 14:44 ` [Caml-list] " Joel Reymont
2007-08-18 14:46   ` Jon Harrop
2007-08-19 10:26   ` Nicolas Pouillard
2007-08-18 14:54 ` skaller
2007-08-20 22:55   ` Nathaniel Gray
2007-08-22  8:54     ` Adrien

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).