From: Vincent Poirriez <Vincent.Poirriez@univ-valenciennes.fr>
To: Jean.Luc.Paillet@lim.univ-mrs.fr
Cc: caml-list@inria.fr
Subject: Re: sequencing
Date: Tue, 03 Jun 1997 15:47:22 +0100 [thread overview]
Message-ID: <33942E7A.7C54@univ-valenciennes.fr> (raw)
In-Reply-To: <9706030959.AA15419@gyptis.univ-mrs.fr>
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'
il n'est pas inversé puisqu'il n'est pas spécifié!!!
Le sens naturel, pour nous autres, est l'évaluation de gauche à droite,
ce n'est pas le même dans d'autres cultures et certaines raisons
(efficacité de compilation par exmple) font que certains compilateurs
évaluent de droite à gauche, c'est le cas pour ocaml.
Si vous voulez controler l'ordre d'exécution utilisez l'opérateur
de séquencement ";" ou, ce qui est équivalent pour votre exemple, les
itérateurs "impératifs"
Ce qui donne
Objective Caml version 1.05
# List.iter;;
- : ('a -> 'b) -> 'a list -> unit = <fun>
# List.iter print_string ["a" ; "b"];;
ab- : unit = ()
# let rec iter f = function
[] -> ()
| h::t -> f h; iter f t;;
val iter : ('a -> 'b) -> 'a list -> unit = <fun>
# iter print_string ["a" ; "b"];;
ab- : unit = ()
#
au lieu de votre code
> 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) ;;
>
> On pourrait s'attendre ici a ce que "(f head)" soit evalue avant
> l'appel recursif. He bien non,
> on obtient encore
>
> monmap print_string ["a" ; "b"] ;;
> ba- : unit list = [(); ()]
>
> Quelle est mon erreur d'interpretation ?
> Doit-on penser que (f head) est empile jusqu'a ce que la fonction termine ?
>
> ?????
> **********************************************
> ££ English version ££
>
> I don't understand why the sequencing of side effects seems to be inverted,
It is not inverted is it is not specified!!!
The natural way, "for us" is to evaluate from left to right
but it is not the same in other cultures and for some
deep reasons (efficiency for example) some compilers evaluate
from right to left, it is the case for ocaml.
If you need to control the order of evaluation you have to use
the operator ";" or, which is simpler for your example,
the predifined "imperatif" iterator:
This leads to
Objective Caml version 1.05
# List.iter;;
- : ('a -> 'b) -> 'a list -> unit = <fun>
# List.iter print_string ["a" ; "b"];;
ab- : unit = ()
# let rec iter f = function
[] -> ()
| h::t -> f h; iter f t;;
val iter : ('a -> 'b) -> 'a list -> unit = <fun>
# iter print_string ["a" ; "b"];;
ab- : unit = ()
#
instead of your code
>
> 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) ;;
>
> We also obtain
>
> monmap print_string ["a" ; "b"] ;;
> ba- : unit list = [(); ()]
>
> We could thing that "(f head)" is evaluated before the recursive call .
>
> What is my mistake ?
> Does the term "(f head)" pushed on the recursion stack until terminating
> the recursive calls, before being evaluated ?
>
> Thanks for an explanation
>
> Kind regards
>
> Jean-Luc Paillet
>
Vincent Poirriez
--
Tel: (33) {0}3 27 14 13 33 Fax: (33) {0}3 27 14 12 94
mailto:Vincent.Poirriez@univ-valenciennes.fr
http://www.univ-valenciennes.fr/limav/poirriez
ISTV Université de Valenciennes Le Mont Houy BP 311 F59304 Valenciennes
CEDEX
prev parent reply other threads:[~1997-06-03 15:16 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-06-03 9:04 sequencing Jean.Luc.Paillet
1997-06-03 11:14 ` sequencing Francois Pessaux
1997-06-03 11:15 ` sequencing Jean-Christophe Filliatre
1997-06-03 11:56 ` sequencing Michel Quercia
1997-06-03 14:47 ` Vincent Poirriez [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=33942E7A.7C54@univ-valenciennes.fr \
--to=vincent.poirriez@univ-valenciennes.fr \
--cc=Jean.Luc.Paillet@lim.univ-mrs.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox