Discussion:
reverse a list
Roelof Wobben
2014-10-19 12:07:07 UTC
Permalink
Helllo,

Im a beginner which tries to do the 99 ocaml problems.

Now I try to reverse a list.
Does I need to use two list . one for the old one and one for the
reversed list.

Roelof
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Gabriel Scherer
2014-10-19 12:30:56 UTC
Permalink
Yes.

As an intermediate step, you should consider implementing the function
reverse_append : 'a list -> 'a list -> 'a list
such that (reverse_append li1 li2) adds li1 *reversed* on top of li2. For
example, reverse_append [1;2] [3;4] is [2;1;3;4].

Is there a particular reason why you're not using the ocaml_beginners list
anymore? It's a good list for this kind of questions and, if you perceived
a problem with it, it would be interesting to have some feedback about it.
Post by Roelof Wobben
Helllo,
Im a beginner which tries to do the 99 ocaml problems.
Now I try to reverse a list.
Does I need to use two list . one for the old one and one for the reversed
list.
Roelof
--
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Malcolm Matalka
2014-10-19 12:34:10 UTC
Permalink
Lists are immutable so you'll be constructing a list as you consume
another list.
Post by Roelof Wobben
Helllo,
Im a beginner which tries to do the 99 ocaml problems.
Now I try to reverse a list.
Does I need to use two list . one for the old one and one for the reversed list.
Roelof
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Philippe Wang
2014-10-19 13:59:41 UTC
Permalink
Post by Malcolm Matalka
Lists are immutable so you'll be constructing a list as you consume
another list.
Please don't formulate it this way, as it sounds to me as if the list
being "consumed" is disappearing (because that's what generally
happens when something is being "consumed"). :-/
To the beginners reading this: well, in OCaml, any given list will
never disappear before it actually becomes useless, and they may be
"consumed" over and over again without any "harm" happening to them.
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Roelof Wobben
2014-10-19 15:13:15 UTC
Permalink
Post by Philippe Wang
Post by Malcolm Matalka
Lists are immutable so you'll be constructing a list as you consume
another list.
Please don't formulate it this way, as it sounds to me as if the list
being "consumed" is disappearing (because that's what generally
happens when something is being "consumed"). :-/
To the beginners reading this: well, in OCaml, any given list will
never disappear before it actually becomes useless, and they may be
"consumed" over and over again without any "harm" happening to them.
Thanks all

Now trying to find out how I can add things to the second list. I think
this can work 2::l2

Roelof
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Roelof Wobben
2014-10-19 15:37:20 UTC
Permalink
Post by Roelof Wobben
Post by Philippe Wang
Post by Malcolm Matalka
Lists are immutable so you'll be constructing a list as you consume
another list.
Please don't formulate it this way, as it sounds to me as if the list
being "consumed" is disappearing (because that's what generally
happens when something is being "consumed"). :-/
To the beginners reading this: well, in OCaml, any given list will
never disappear before it actually becomes useless, and they may be
"consumed" over and over again without any "harm" happening to them.
Thanks all
Now trying to find out how I can add things to the second list. I
think this can work 2::l2
Roelof
I have found a way :

let rec test l1 l2 =
match l1 with
| [] -> l2
| h :: t -> test t (h::l2)
;;

Roelof
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
SF Markus Elfring
2014-10-19 12:50:33 UTC
Permalink
Post by Roelof Wobben
Now I try to reverse a list.
Would you like to compare the run time properties for different approaches?
1. Use of immutable data structures in the functional programming style

2. Manage a list as a mutable member of a class in an "imperative" and
object-oriented programming style

Regards,
Markus
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Loading...