Re: reverse a list. is this a good solution

"Roelof Wobben [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Yes, I noticed and I try to understand
the difference :

I think you mean this function :

let rec aux_rev l accum =

DoI understand that this part in aux_rev[] takes care of a empty
acc ?

I find this solution a little bit confusing expecially the in part
.

Roelof

Matthieu Dubuget [email protected] [ocaml_beginners]
schreef op 20-10-2014 19:42:

Have a second look at Chingfan Tsao post.

He gives you the solution.

Yes: l2 was renamed to accum. This is because it is an
accumulator that is growing during the calls, and returns
the result at the end.

You'll notice that inside rev_list_tail_rec, a hidden
function is defined, and called. During this call, accum
is initialized.

Best regards

Le 20/10/2014 17:56, Roelof
Wobben [email protected]
[ocaml_beginners] a écrit :

Oke,

If I look it it seems to me that acc does the same as
l2 on my programm.

But does acc not have to be intialized on some way ?

Sébastien

then the outcome will be :

string list = ["c"; "b"; "a"; "1"; "2"; "3"]

so it reverse the second string and not the first one.

Roelof

Chingfan Tsao [email protected]
[ocaml_beginners] schreef op 20-10-2014 15:55:

Hi,

It is indeed a good way to solve it since
it's tail recursion, however, there's one more
thing to do to make it easier, wrap it with a
more convenient function:

let rev_list_tail_rec l =

let rec aux_rev l accum =

match l with

| [] -> accum

| h :: tl ->

aux_rev tl (h::accum)

in

aux_rev l []

this way, you can just use rev_list_tail_rec
l instead of having to give one more [] as the
initial value of the accumulator.

2014-10-20 18:56
GMT+08:00 Roelof Wobben [email protected]
[ocaml_beginners] <[email protected] >:

Hello,

I try the 99 ocaml problems and have
solved the reverse a list problem.

My solution looks like this :

let rec test l1 l2 =

match l1 with

| [] -> l2

| h :: t -> test t (h::l2)

;;

Is this a good way to solve it or
are there things that can be better
?

Roelof

--

曹竞帆

哈尔滨工业大学计算机科学与技术专业本科在读

哈尔滨工业大学IBMTC成员

--
Matthieu Dubuget
Guide d’autodéfense numérique : http://guide.boum.org

__._,_.___

----------

Posted by: Roelof Wobben <[email protected]>
----------

Reply via web post

•

Reply to sender

•

Reply to group

•

Start a New Topic

•

Messages in this topic
(6)

Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners

The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr

Attachments are banned and you're asked to be polite, avoid flames etc.

Visit Your Group

-
New Members
3

https://groups.yahoo.com/neo;_ylc=X3oDMTJkNjQzbTNxBF9TAzk3NDc2NTkwBGdycElkAzQ5OTkxOTQEZ3Jwc3BJZAMxNzA1MDA2NzY0BHNlYwNmdHIEc2xrA2dmcARzdGltZQMxNDEzODI3Mzg2

• Privacy • Unsubscribe • Terms of Use

.

__,_._,___
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.