From: "Kontra, Gergely" <kgergely@mlabdial.hit.bme.hu>
To: Brian Hurt <brian.hurt@qlogic.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Probably FAQ: Why is list-append (list :: elem) so expensive?
Date: Thu, 26 Sep 2002 16:44:00 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.21.0209261637260.19322-100000@mlabdial.hit.bme.hu> (raw)
In-Reply-To: <Pine.LNX.4.33.0209250958520.1974-100000@eagle.ancor.com>
>I get that the cost of list concatenation is proportional to the length of
>the first list. So (elem :: list) is O(1) no matter what the length of
>list is. But (list :: elem) is O(n), with n being the length of the list.
[...]
>An example of where this would come in usefull. Consider the merge
>portion of a merge sort (yes, I know I'm duplicating code that is in the
>List module- I'm doing it so that I can learn the language. Plus, here we
>all understand the problem). The current implementation I have (probably
>chock full of inefficiencies) is:
If you want to build up a list from the beginning to the tail, just
append the new elements before the list, and then reverse the list.
(* Sorry, new to ocaml, my syntax may be SML-ish *)
(* tail-recursive function to reverse a list *)
let reverse List =
(* returns a list like: (reverse L2) @ L1 *)
let rec revapp L1 L2 =
match L2 with
| [] -> L1
| hd::tl -> revapp hd::L1 tl
in revapp [] List
Since reversing is not so slow, this might be, what you want.
Gergő
+-[Kontra, Gergely @ Budapest University of Technology and Economics]-+
| Email: kgergely@mcl.hu, kgergely@turul.eet.bme.hu |
| URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
+-------"Olyan langesz vagyok, hogy poroltoval kellene jarnom!"-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net
-------------------
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
prev parent reply other threads:[~2002-09-26 14:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-25 15:33 Brian Hurt
2002-09-25 15:39 ` Noel Welsh
2002-09-25 15:42 ` Oleg
2002-09-25 16:17 ` Markus Mottl
2002-09-25 18:44 ` Brian Hurt
2002-09-25 19:22 ` Markus Mottl
2002-09-26 7:10 ` Florian Hars
2002-09-26 14:44 ` Kontra, Gergely [this message]
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.LNX.4.21.0209261637260.19322-100000@mlabdial.hit.bme.hu \
--to=kgergely@mlabdial.hit.bme.hu \
--cc=brian.hurt@qlogic.com \
--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