|
List Info
Thread: "ocaml_beginners"::[] Getting last elements of list
|
|
| "ocaml_beginners"::[] Getting
last elements of list |
  Germany |
2007-04-16 00:25:21 |
|
Hi,
is there any way of getting the end of a list in a match case just like
you can get the list head? Or would I have to reverse the list and then
get the head?
Cheers,
Christian
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Getting last elements of list |
  France |
2007-04-16 01:00:48 |
|
Le Mon, 16 Apr 2007 15:25:21 +1000, Christian Lerrahn
< ocaml%40penpal4u.net">ocaml penpal4u.net> a écrit :
> Hi,
> is there any way of getting the end of a list in a match case just
> like you can get the list head? Or would I have to reverse the list
> and then get the head?
You have to reverse the list, unless you define your own list type with
a "pointer" on the first and the last element (it would probably also
allow you to travel along the list in both ways, and might be a good
idea depending on your problem).
Regards,
--
Gabriel
[Non-text portions of this message have been removed]
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Getting last elements of list |
  France |
2007-04-16 01:48:28 |
|
Gabriel Kerneis a écrit :
> Le Mon, 16 Apr 2007 15:25:21 +1000, Christian Lerrahn
> < ocaml%40penpal4u.net">ocaml penpal4u.net> a écrit :
>
>> Hi,
>> is there any way of getting the end of a list in a match case just
>> like you can get the list head? Or would I have to reverse the list
>> and then get the head?
>>
>
> You have to reverse the list, unless you define your own list type with
> a "pointer" on the first and the last element (it would probably also
> allow you to travel along the list in both ways, and might be a good
> idea depending on your problem).
>
> Regards,
>
Doubly-linked lists would not be purely functional. Use with care.
Salutations
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Getting last elements of list |
  United Kingdom |
2007-04-16 02:02:48 |
|
On Monday 16 April 2007 06:25, Christian Lerrahn wrote:
> Hi,
> is there any way of getting the end of a list in a match case just like
> you can get the list head? Or would I have to reverse the list and then
> get the head?
You can write a function to decapitate the last element:
# let last list = match rev list with
| [] -> raise Not_found
| b::f -> b, rev f;;
val last : 'a list -> 'a * 'a list
If you're doing this a lot then you either want to keep the list reversed or
use a different data structure.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Getting last elements of list |
  United States |
2007-04-16 19:23:34 |
|
On Mon, 16 Apr 2007, Christian Lerrahn wrote:
> Hi,
> is there any way of getting the end of a list in a match case just like
> you can get the list head? Or would I have to reverse the list and then
> get the head?
It's fairly easy to write a function to get the last element of the list:
let rec last = function
| [] -> invalid_arg "You called last on an empty list!"
| h :: [] -> h
| h :: t -> last t
;;
The problem with this is that it walks the whole list- if the list is a
million elements long, you've got to walk the whole million elements to
get the last one.
If you're regularly needing to get the last element of the list, what you
need is a dequeue. I recommend reading Chris Okasaki's "Purely Functional
Data Structures", and picking one that's appropriate.
Brian
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Getting last elements of list |
  Germany |
2007-04-16 20:06:45 |
|
On Mon, Apr 16, 2007 at 03:25:21PM +1000, Christian Lerrahn wrote:
> Hi,
> is there any way of getting the end of a list in a match case just like
> you can get the list head? Or would I have to reverse the list and then
> get the head?
>
[...]
If you create that list by yourself,
you could remember the first element you
insert into the list 
As the "::" command put's the new element at the head
of the list, the first element you insert into the
list, is the last element on the list.
Maybe this might help you, maybe not.
Ciao,
Oliver
__._,_.___
.
__,_._,___
|
[1-6]
|
|