caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: ijtrotts@ucdavis.edu (Issac Trotts)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] function
Date: Thu, 5 Dec 2002 11:00:50 +0100 (MET)	[thread overview]
Message-ID: <200212051000.LAA22469@pauillac.inria.fr> (raw)
In-Reply-To: <20021203232211.GC22545@beech> from Issac Trotts at "Dec 3, 102 03:22:11 pm"

> On Mon, Dec 02, 2002 at 04:55:06PM +0100, altavillasalvatore@libero.it wrote:
> > Hi all,
> > I would want to know if a function exists that allows me to make this:
> > 
> > ["123";"45";"678"]  ->  [1;2;3;4;5;6;7;8;].
> 
> let f l = 
>   let ret = ref [] in
>   String.iter 
>     (fun c -> ret := ((int_of_char c) - (int_of_char '0')) :: !ret) 
>     (List.fold_left (^) "" l);
>   List.rev !ret;;
>   
> f ["123";"45";"678"];;
> 
> Cheers,
> Issac Trotts

If you like puzzles, here is an additional exercise:

00) List which parens are useless in this code, according to which
    rules given in the simple guide-lines and syntactic conventions of
    http://pauillac.inria.fr/caml/FAQ/qrg-eng.html#parens and
    http://pauillac.inria.fr/caml/FAQ/pgl-eng.html#parens.
0)  Rewrite this code to a more suitable form for a beginner,
    i.e. in a purely functional style with no side effect.
1)  Try to avoid the useless List.rev at the end.
2)  What is the complexity of your function f ?
3)  Explain why the List.fold_left application is not only over
    complex but also runtime over consuming.

Hope this helps.

Cheers,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-12-05 10:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-02 15:55 altavillasalvatore
2002-12-02 16:29 ` sebastien FURIC
2002-12-02 17:35 ` Oleg
2002-12-03 23:22 ` Issac Trotts
2002-12-05 10:00   ` Pierre Weis [this message]
2002-12-05 20:24     ` Issac Trotts
2002-12-05 23:00       ` Oleg
2002-12-06 21:31         ` Issac Trotts
2002-12-07 10:28           ` Xavier Leroy
2002-12-07 17:31             ` Oleg
2002-12-05 20:46     ` Oleg
2002-12-05 21:06       ` Pal-Kristian Engstad
2002-12-06  7:38 Jeremy Fincher

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=200212051000.LAA22469@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=ijtrotts@ucdavis.edu \
    /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;
as well as URLs for NNTP newsgroup(s).