caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Diego Olivier Fernandez Pons <Diego-Olivier.FERNANDEZ-PONS@cicrp.jussieu.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] @, List.append, and tail recursion
Date: Fri, 31 Jan 2003 18:32:41 +0100 (NFT)	[thread overview]
Message-ID: <Pine.A41.4.44.0301311831000.4055078-100000@ibm1.cicrp.jussieu.fr> (raw)

    Bonjour,

Brian Hurt wrote :

> Here's an example I have run across.  I'm working with sparse vectors, and
> basically storing them as (int * float) lists.  Now, let's write the
> vector add function.  The naive implementation would be:

let rec add_rec result = fun a b ->
  match (a, b) with
    | ([], _) -> a @ result
    | (_, []) -> b @ result
    | ((n, x) :: a_tail, (m, y) :: b_tail) ->
       match compare m n with
         | k when k < 0 -> add_rec ((n, x) :: result) a_tail b
         | k when k > 0 -> add_rec ((m, y) :: result) a b_tail
         | _ -> add_rec ((n, x +. y) :: result) a_tail b_tail


This is a tail recursive version of your function. The main problem is
that it reverses the list every time it is called.

Your first idea is to define

let add = fun a b -> List.rev (add_rec a b)

and you are right when you say that you are allocating a lot of
memory. But you can do much better : add_rec only needs the two lists
to be sorted, not to be increasing.

Why don't you try

type vector =
  | Increasing of (int * float) list
  | Decreasing of (int * float) list

then you just have to write the 4 corresponding add functions

Here is a second idea : is there a way to implement add in such a way
that it does not need the two arguments to be sorted ? If you were
working with arrays this would not be difficult.

Then, just try functional random acces lists. They give you a O(1)
access to the head element (in case both list are sorted in the same
way) or O(log n) acces to any element.


        Diego Olivier


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


             reply	other threads:[~2003-01-31 17:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-31 17:32 Diego Olivier Fernandez Pons [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-01-31 22:27 Harrison, John R
2003-01-31 19:58 Harrison, John R
2003-01-31 21:04 ` Brian Hurt
2003-01-24 15:35 Andrew Kennedy
2003-01-30  1:44 ` brogoff
2003-01-30  9:57   ` Christophe Raffalli
2003-01-30 16:03     ` Brian Hurt
2003-01-31 10:33     ` Mattias Waldau
2003-01-24  0:48 Brian Hurt
2003-01-30 18:10 ` Olivier Andrieu
2003-01-30 19:46   ` Brian Hurt
2003-01-30 20:52     ` Olivier Andrieu
2003-01-30 21:57       ` Brian Hurt
2003-01-31  2:16         ` james woodyatt
2003-01-31 17:05           ` Diego Olivier Fernandez Pons
2003-01-31 19:52             ` Brian Hurt
2003-01-31 21:34             ` Issac Trotts
2003-01-31 17:13           ` Brian Hurt
2003-01-31 17:42             ` brogoff
2003-01-31 19:18             ` Russ Ross
2003-01-31 19:32               ` Alexander V. Voinov
2003-02-01  2:30               ` brogoff
2003-01-31 23:12             ` Issac Trotts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.A41.4.44.0301311831000.4055078-100000@ibm1.cicrp.jussieu.fr \
    --to=diego-olivier.fernandez-pons@cicrp.jussieu.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).