From: Florian Hars <hars@bik-gmbh.de>
To: Joel Christner <joel.christner@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Newbie list question
Date: Fri, 15 May 2009 15:49:45 +0200 [thread overview]
Message-ID: <4A0D72F9.8020409@bik-gmbh.de> (raw)
In-Reply-To: <9da743ed0905131024u53a207f0s9e963be782f3f2c6@mail.gmail.com>
Joel Christner schrieb:
> What I'm wanting to do is create a ref list that contains a series of
> strings
Judging from your code, you really do not want to do this (I just sped up
a program of mine by at least two orders of magnitude fixing an error like
that). What you probably want to do is
module S = Set.Make(String);;
let s = ref S.empty;;
let add_variable_ref s v =
s := S.add v !s;;
add_variable_ref s "foo";;
add_variable_ref s "bar";;
S.elements !s;;
Even better if you can structure your code to get rid of the ref:
# let add_variable s v = S.add v s;;
val add_variable : S.t -> S.elt -> S.t = <fun>
# let vars = List.fold_left add_variable S.empty ["Foo"; "Bar"; "Baz"; "Foo"; "Qux"; "Baz"];;
val vars : S.t = <abstr>
# S.elements vars;;
- : S.elt list = ["Bar"; "Baz"; "Foo"; "Qux"]
Of course, this works as well, but uses more stack space as right folds are
not tail recursive:
# let vars' = List.fold_right S.add ["Foo"; "Bar"; "Baz"; "Foo"; "Qux"; "Baz"] S.empty;;
val vars' : S.t = <abstr>
# S.elements vars';;
- : S.elt list = ["Bar"; "Baz"; "Foo"; "Qux"]
-- Florian.
prev parent reply other threads:[~2009-05-15 13:49 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-13 17:24 Joel Christner
2009-05-13 18:06 ` [Caml-list] " Cedric Auger
2009-05-13 20:24 ` John Li
2009-05-15 13:49 ` Florian Hars [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=4A0D72F9.8020409@bik-gmbh.de \
--to=hars@bik-gmbh.de \
--cc=caml-list@yquem.inria.fr \
--cc=joel.christner@gmail.com \
/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