caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Discarding elements from a list
@ 2003-01-26 19:01 Oliver Bandel
  2003-01-26 19:28 ` Oliver Bandel
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Bandel @ 2003-01-26 19:01 UTC (permalink / raw)
  To: caml-list

Hello,


after Remi has pushed my towards rewriting my
"I do it like I know it, and therefore imperative"
version of a program into a functional version of
this program (thanks, Remi, I have explored a lot
new things since then :)) I sometimes come to
this problem:

I have to use a partial list.

In the reference manual I did not found any
stdlib-function, and so I have two ideas,
of how to perform e.g. a list, which is used
without the head (throwing away first element):

a) Converting the list to an array, shorten the array,
   converting back to a list, using the list in a functional
   manner.

b) Using pattern match to throw away the first element, then
   working in the part after the "->" with a function, which
   works on the rest of the list.

c) using a function, which uses an index, for counting, at which
   position we are inside of the list


This problem arises, when a list, on which should be worked,
will be created and it must be used a part of it (e.g. all
elements but the last or all elements but the first), like
creating a multiplication of the last and the current element...
(like fibonacci uses addition, I need multiplication here)
and when the first or last is necessary for some calculation,
but should be thrown away for some other calculation.

Because I have no criteria to decide, which solution I should
prefer (and why), I always rethink the problem again,
and the decision-process slows down.

So, why should I use which solution?

(And how to select parts of a list in general, not only
 throwing away the first or last element?)

Any hints?


Ciao,
   oliver
-------------------
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


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

* Re: [Caml-list] Discarding elements from a list
  2003-01-26 19:01 [Caml-list] Discarding elements from a list Oliver Bandel
@ 2003-01-26 19:28 ` Oliver Bandel
  2003-01-26 23:27   ` malc
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Bandel @ 2003-01-26 19:28 UTC (permalink / raw)
  To: caml-list

On Sun, Jan 26, 2003 at 08:01:16PM +0100, Oliver Bandel wrote:
[...]
> 
> a) Converting the list to an array, shorten the array,
>    converting back to a list, using the list in a functional
>    manner.
> 
> b) Using pattern match to throw away the first element, then
>    working in the part after the "->" with a function, which
>    works on the rest of the list.
> 
> c) using a function, which uses an index, for counting, at which
>    position we are inside of the list


OK, I've found a very simple solution. :)

OK, it is functional programming.....

OK, it is Ocaml...

OK, I use pattern matching....


OK, this is my solution:



let discard_first lis = match lis with
       []     -> []
     | hd::tl -> tl


So, the main way to get a solution to a program in fp
is simply: write a function for the simplest case,
and then use that function...


The last days I often, over and over again found,
that I most of the times think more complicated,
than a solution in FP is!

Maybe this "think complicated" is an imperative illness.  (?!)

And more and more I see, what the difference between FP and IP is:
It's much more easy than an IP-programmer thought that it is.

It's unbelievable, that I every day see more examples of
how easy programming can be, if it is functional.


nearly unbelievable....

Ciao,
   Oliver
-------------------
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


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

* Re: [Caml-list] Discarding elements from a list
  2003-01-26 19:28 ` Oliver Bandel
@ 2003-01-26 23:27   ` malc
  0 siblings, 0 replies; 3+ messages in thread
From: malc @ 2003-01-26 23:27 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

On Sun, 26 Jan 2003, Oliver Bandel wrote:

> On Sun, Jan 26, 2003 at 08:01:16PM +0100, Oliver Bandel wrote:
> [...]
>
> OK, I've found a very simple solution. :)
>
> OK, it is functional programming.....
>
> OK, it is Ocaml...
>
> OK, I use pattern matching....
>
>
> OK, this is my solution:
>
>
>
> let discard_first lis = match lis with
>        []     -> []
>      | hd::tl -> tl
>
Good! But how about List.tl?

-- 
mailto:malc@pulsesoft.com
-------------------
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


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

end of thread, other threads:[~2003-01-26 23:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-26 19:01 [Caml-list] Discarding elements from a list Oliver Bandel
2003-01-26 19:28 ` Oliver Bandel
2003-01-26 23:27   ` malc

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