English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Feature Wish: List comprehensions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-03-30 (08:38)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: [Caml-list] Feature Wish: List comprehensions

I'm having a small problem trying to tidily define an equivalent
function to some Haskell code.

pThen combine p1 p2 toks
  = [ (combine v1 v2, toks2) | (v1,toks1) <- p1 toks, (v2, toks2) <- p2 toks1 ]

(I'll also be needing to specify equivalents for pThen3 & pThen4).

The first isn't too bad...

let pThen combine p1 p2 = fun ts ->
  let l1 = p1 ts in
  let l2 = List.map (fun (v1,ts1) -> p2 ts1) l1 in
    (List.map2 (fun (v1,ts1) l2 -> List.map (fun (v2,ts2) -> combine
v1 v2,ts2) l2) l1 l2)

As you can see, extending this style to pThen3 & pThen4 is going to be
very, very ugly (and hard to get right first time).

I know it's just shorthand for map/filter, (and more complex
derivatives of these), but it would be nice to have in ocaml...

BTW: any suggestions on a better way to write the above function would
be appreciated ;-)

Kindest Regards,

Jonathan Roewen