Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] @, List.append, and tail recursion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Issac Trotts <ijtrotts@u...>
Subject: Re: [Caml-list] @, List.append, and tail recursion
On Fri, Jan 31, 2003 at 06:05:30PM +0100, Diego Olivier Fernandez Pons wrote:
>     Bonjour,
> 
> Some comments on various contributions to this thread
> 
> Brian Hurt wrote :
> 
> > I'm basically doing sparse vector/matrix stuff, handling
> > (effectively) (colno * value) list for vectors, and (rowno * vector)
> > list for matrix.  And I may be hitting lists long enough to trip the
> > problem.
> 
> If you are doing sparse matrix operations and you still hit lists long
> enough to cause a stack overflow, then your matrix must be really
> huge.
> 
> If the ordrer of the terms does not matter (or if you can manage with
> the position information you keep in your sparse matrix) then you just
> need to write tail recursive functions, not taking care of the list
> being reversed
> 
> let rec rev_map f result = function
>   | [] -> result
>   | head :: tail -> rev_map (f head) tail

This doesn't work on OCaml 3.06:

  "This expression has type 'a but is here used with type 'b -> 'a"

How about 

  let rec rev_map f result = function
    | [] -> result
    | head :: tail -> rev_map f (f head :: result) tail;; 

  # map ((+) 3) [] [1;2;3;4;5];;
  - : int list = [8; 7; 6; 5; 4]

Issac

[snip]

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