From: Remi VANICAT <vanicat@labri.u-bordeaux.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Great Beginner
Date: 12 Nov 2001 07:39:09 +0100 [thread overview]
Message-ID: <877kswebn6.dlv@wanadoo.fr> (raw)
In-Reply-To: <005c01c16b0a$3971f860$2d2ce8d4@Utilisateur>
"Diego Olivier Fernandez Pons" <FernandezPons@iFrance.com> writes:
> Rémi Vanicat a proposé le code suivant :
>
> let rec somme n =
> if n = 0 then 0
> else n + (somme (n - 1))
>
> let somme n =
> let rec aux i x =
> if i <= n then
> aux (i+1) (x + i)
> else x in
> aux 1 0
>
> Ce code reste néanmoins très proche du code impératif. Dans un style
> beacoup plus fonctionnel on aura plutôt :
>
> let rec somme = function
> | 0 -> 0
> | k -> k + somme (k-1)
> ;;
j'ai du mal a voir la différence entre ceci et mon code. le if then
else du caml est une construction fonctionnel.
[..]
> Enfin, je vous recommande de « commenter » votre code par des noms de
> variables explicites (comme total ou k plutôt que aux, acc, etc.) ;
> cela permet un code immédiatement compréhensible.
je vous l'accorde, mais c'est une question d'habitude ici
>
> La seule remarque supplémentaire que l'on pourrait faire est que dans
> les langages fonctionnels on préfère les fonctions à plusieurs
> arguments que les fonctions à arguments dans les produits cartésiens
> (ce qui ici n'est guère difficile car vous ne faites aucun test sur la
> variable total)
>
> let somme = function n ->
> let rec sommeCPS = function total -> function
> | 0 -> total
> | k -> sommeCPS (total + k) (k - 1)
> in
> sommeCPS 0 n
> ;;
et en caml, on préférera (c'est quand même plus lisible) :
let somme n =
let rec sommeCPS total = function
| 0 -> total
| k -> sommeCPS (total + k) (k - 1)
in somme CPS 0 n
les "function" partout ne rajoute absolument rien a la lisibilité,
voir même en enlève, l'information importante étant noyé au milieux de
nombreux mot clé.
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-11-12 6:37 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-09 13:44 Franck
2001-11-11 11:34 ` Remi VANICAT
2001-11-10 15:29 ` Diego Olivier Fernandez Pons
2001-11-12 6:39 ` Remi VANICAT [this message]
2001-11-12 8:13 ` Diego Olivier Fernandez Pons
-- strict thread matches above, loose matches on Subject: below --
2001-11-09 14:10 Krishnaswami, Neel
2001-11-09 11:08 Franck
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=877kswebn6.dlv@wanadoo.fr \
--to=vanicat@labri.u-bordeaux.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