From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA02449 for caml-redistribution; Tue, 3 Jun 1997 17:15:44 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA00660 for ; Tue, 3 Jun 1997 13:57:55 +0200 (MET DST) Received: from enst.enst.fr (DmQptlhXEiAGB8TcDK474zwdKTtrfkUK@enst.enst.fr [137.194.2.16]) by nez-perce.inria.fr (8.8.5/8.7.3) with ESMTP id NAA21742 for ; Tue, 3 Jun 1997 13:57:52 +0200 (MET DST) Received: from liszt.enst.fr (liszt.enst.fr [137.194.4.32]) by enst.enst.fr (8.8.4/8.8.4) with SMTP id NAA18170; Tue, 3 Jun 1997 13:57:41 +0200 (MET DST) Received: from Montchapet (remote11.ac-dijon.fr) by liszt.enst.fr (MX V4.2 AXP) with SMTP; Tue, 03 Jun 1997 13:56:24 MET_DST Message-ID: <33940678.4F77ED0B@cal.enst.fr> Date: Tue, 03 Jun 1997 13:56:40 +0200 From: Michel Quercia X-Mailer: Mozilla 3.0 (X11; I; Linux 2.0.27 i486) MIME-Version: 1.0 To: Jean.Luc.Paillet@lim.univ-mrs.fr CC: caml-list@inria.fr Subject: Re: sequencing References: <9706030959.AA15419@gyptis.univ-mrs.fr> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: weis Jean.Luc.Paillet@lim.univ-mrs.fr wrote: > > Quelqu'un pourrait-il m'expliquer pourquoi le sequencement d'une fonction > a` effet de bord, telle que "print_string" par exemple, semble inverse' > quand elle est appliquée sur une liste au moyen d'un iterateur. > > Par exemple > map print_string ["a" ; "b"] ;; produit > > ba- : unit list = [(); ()] > ce qui implique un parcours de la liste de droite a gauche . > Plus curieux, avec une definition recursive "personnelle" de l'iterateur, > telle que > > let rec monmap f liste = > match liste with [] -> [] | > head :: tail -> (f head) :: monmap f (tail) ;; Un coup d'oeil a la bibliotheque sur les listes (/usr/local/lib/caml-light/list.ml) monter que map est a peu pres defini comme cela. > > On pourrait s'attendre ici a ce que "(f head)" soit evalue avant > l'appel recursif. He bien non, L'ordre d'evaluation de (expr_a) :: (expr_b) n'est pas specifie dans le manuel de reference. En pratique, il se trouve que expr_b est evalue d'abord. Voici une fonction lr_map produisant une application de gauche a droite : > Caml Light version 0.73 #map print_char [`a`; `b`; `c`];; cba- : unit list = [(); (); ()] #let rec lr_map f liste = match liste with | [] -> [] | t::q -> let t' = f(t) in t' :: (lr_map f q) ;; lr_map : ('a -> 'b) -> 'a list -> 'b list = #lr_map print_char [`a`; `b`; `c`];; abc- : unit list = [(); (); ()] # > ********************************************** > ££ English version ££ > > I don't understand why the sequencing of side effects seems to be inverted, > when using "map" , like for example in the following: > > map print_string ["a" ; "b"] ;; gives > ba- : unit list = [(); ()] > > it means that the list is scanned from the right to the left > > More surprising, with a recursive hand made definition of "map", such as > > let rec monmap f liste = > match liste with [] -> [] | > head :: tail -> (f head) :: monmap f (tail) ;; > Look at the list implementation (/usr/local/lib/caml-light/list.ml), you'll see that map is roughly defined that way. > > We could thing that "(f head)" is evaluated before the recursive call . > The order of evaluation of (expr_a) :: (expr_b) is unspecified in the reference manual. Actually, it turns out that expr_b is evaluated first. Here is a function lr_map going from left to right : > Caml Light version 0.73 #map print_char [`a`; `b`; `c`];; cba- : unit list = [(); (); ()] #let rec lr_map f liste = match liste with | [] -> [] | t::q -> let t' = f(t) in t' :: (lr_map f q) ;; lr_map : ('a -> 'b) -> 'a list -> 'b list = #lr_map print_char [`a`; `b`; `c`];; abc- : unit list = [(); (); ()] # -- Michel Quercia Lycee Carnot 16 bd Thiers 21000 Dijon mailto:quercia@cal.enst.fr