caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] reverse a list
@ 2014-10-19 12:07 Roelof Wobben
  2014-10-19 12:30 ` Gabriel Scherer
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Roelof Wobben @ 2014-10-19 12:07 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 12:07 [Caml-list] reverse a list Roelof Wobben
@ 2014-10-19 12:30 ` Gabriel Scherer
  2014-10-19 12:34 ` Malcolm Matalka
  2014-10-19 12:50 ` SF Markus Elfring
  2 siblings, 0 replies; 7+ messages in thread
From: Gabriel Scherer @ 2014-10-19 12:30 UTC (permalink / raw)
  To: Roelof Wobben; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 986 bytes --]

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.

On Sun, Oct 19, 2014 at 2:07 PM, Roelof Wobben <r.wobben@home.nl> wrote:

> 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
>

[-- Attachment #2: Type: text/html, Size: 1715 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 12:07 [Caml-list] reverse a list Roelof Wobben
  2014-10-19 12:30 ` Gabriel Scherer
@ 2014-10-19 12:34 ` Malcolm Matalka
  2014-10-19 13:59   ` Philippe Wang
  2014-10-19 12:50 ` SF Markus Elfring
  2 siblings, 1 reply; 7+ messages in thread
From: Malcolm Matalka @ 2014-10-19 12:34 UTC (permalink / raw)
  To: Roelof Wobben; +Cc: caml-list

Lists are immutable so you'll be constructing a list as you consume
another list.

Roelof Wobben <r.wobben@home.nl> writes:

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 12:07 [Caml-list] reverse a list Roelof Wobben
  2014-10-19 12:30 ` Gabriel Scherer
  2014-10-19 12:34 ` Malcolm Matalka
@ 2014-10-19 12:50 ` SF Markus Elfring
  2 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2014-10-19 12:50 UTC (permalink / raw)
  To: Roelof Wobben; +Cc: caml-list

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 12:34 ` Malcolm Matalka
@ 2014-10-19 13:59   ` Philippe Wang
  2014-10-19 15:13     ` Roelof Wobben
  0 siblings, 1 reply; 7+ messages in thread
From: Philippe Wang @ 2014-10-19 13:59 UTC (permalink / raw)
  To: Malcolm Matalka; +Cc: Roelof Wobben, OCaml Mailing List

On Sun, Oct 19, 2014 at 2:34 PM, Malcolm Matalka <mmatalka@gmail.com> wrote:
> 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 13:59   ` Philippe Wang
@ 2014-10-19 15:13     ` Roelof Wobben
  2014-10-19 15:37       ` Roelof Wobben
  0 siblings, 1 reply; 7+ messages in thread
From: Roelof Wobben @ 2014-10-19 15:13 UTC (permalink / raw)
  To: caml-list

Philippe Wang schreef op 19-10-2014 15:59:
> On Sun, Oct 19, 2014 at 2:34 PM, Malcolm Matalka <mmatalka@gmail.com> wrote:
>> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] reverse a list
  2014-10-19 15:13     ` Roelof Wobben
@ 2014-10-19 15:37       ` Roelof Wobben
  0 siblings, 0 replies; 7+ messages in thread
From: Roelof Wobben @ 2014-10-19 15:37 UTC (permalink / raw)
  To: caml-list

Roelof Wobben schreef op 19-10-2014 17:13:
> Philippe Wang schreef op 19-10-2014 15:59:
>> On Sun, Oct 19, 2014 at 2:34 PM, Malcolm Matalka <mmatalka@gmail.com> 
>> wrote:
>>> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-10-19 15:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-19 12:07 [Caml-list] reverse a list Roelof Wobben
2014-10-19 12:30 ` Gabriel Scherer
2014-10-19 12:34 ` Malcolm Matalka
2014-10-19 13:59   ` Philippe Wang
2014-10-19 15:13     ` Roelof Wobben
2014-10-19 15:37       ` Roelof Wobben
2014-10-19 12:50 ` SF Markus Elfring

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).