caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pal-Kristian Engstad <engstad@naughtydog.com>
To: Oleg <oleg_inconnu@myrealbox.com>,
	Pierre Weis <pierre.weis@inria.fr>,
	ijtrotts@ucdavis.edu (Issac Trotts)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] function
Date: Thu, 5 Dec 2002 13:06:44 -0800	[thread overview]
Message-ID: <200212051306.44106.engstad@naughtydog.com> (raw)
In-Reply-To: <200212051546.56416.oleg_inconnu@myrealbox.com>

On Thursday 05 December 2002 12:46 pm, Oleg wrote:
> To help the original poster earn extra credit, I'll write the function in
> the three languages I [sort of] know:
>
> typedef std::list<char*> LS;
> typedef std::list<int> LI;
>
> void f(LS& input, LI& output) {
>     for(LS::iterator i = input.begin(); i != input.end(); ++i)
>         for(char* p = *i; *p != '\0'; ++p)
>             output.push_back(int(*p) - int('0'));
> }

Isn't there a foreach() algorithm in STL?

> let rec f_aux lst i acc =
>     match lst with
>
>     | [] -> acc
>     | x :: y when i = String.length x -> f_aux y 0 acc
>     | x :: y -> f_aux lst (i + 1) (digit x.[i] :: acc)
>
> let f lst = List.rev (f_aux lst 0 [])

Ah, the use of List.rev... :-) Of course, without it, it is hard. Here's my two 
attempts:

let f4 strs = 
  let to_ord c = int_of_char c - int_of_char '0' in
  let rec strings_to_list = function
    | [] -> []
    | str :: strs ->
	let rec to_list s k n = 
	  if k = n then strings_to_list strs else to_ord s.[k] :: to_list s (k + 1) n
	in
	  to_list str 0 (String.length str) 
  in
    strings_to_list strs

This one is unfortunately not tail recursive, but it is neat in the sense that it doesn't
use any library function except int_of_char.

let f5 strs = 
  let to_ord c = int_of_char c - int_of_char '0' in
  let rec strings_to_list acc = function
    | [] -> acc
    | str :: strs ->
	let rec to_list acc s k n =
	  if k = n then strings_to_list acc strs else to_list (to_ord s.[k] :: acc) s (k + 1) n
	in
	  to_list acc str 0 (String.length str)
  in
    List.rev(strings_to_list [] strs)

Equivalent to Oleg's I believe?

PKE.
-- 
  _       
  \`.       Pål-Kristian Engstad, Senior Software Engineer,
   \ `|     Naughty Dog, Inc., 1315 3rd Street Promenade, 
  __\ |`.   Santa Monica, CA 90046, USA. (310) 752-1000 x799. 
    /  /o   mailto:engstad@naughtydog.com http://www.naughtydog.com
   /  '~    mailto:mrengstad@yahoo.com    http://www.engstad.com
  / ,'      Hang-gliding Rulez!
  ~'

-------------------
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 21:04 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
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 [this message]
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=200212051306.44106.engstad@naughtydog.com \
    --to=engstad@naughtydog.com \
    --cc=caml-list@inria.fr \
    --cc=ijtrotts@ucdavis.edu \
    --cc=oleg_inconnu@myrealbox.com \
    --cc=pierre.weis@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;
as well as URLs for NNTP newsgroup(s).