caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: Valentin ROBERT <valentin.robert.42@gmail.com>,
	Martin DeMello <martindemello@gmail.com>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] is there a more concise way to write this?
Date: Fri, 20 Jan 2012 08:37:08 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9C264BA49@Remus.metastack.local> (raw)
In-Reply-To: <CAC1A9D40NusKirW7DEQbzuhtXvuVwqX7VBR6v6trJ1JjbPO2Bg@mail.gmail.com>

Valentin ROBERT wrote:
> I guess you can write it like:
> 
> let a = (if out then [o] else []) @ (if value then [v] else [])
>
> But it's not particularly more pleasant to the eye.
> Still it reduces the exponential explosion of the code, at a small additional cost (the @), I believe.

Actually, it's possible that with more cases it might be faster - it's eliminating the allocation (at some point) of all the tuples needed for the match case, it potentially eliminates a lot of linear comparisons to find the correct match case (I don't think that the compiler would be able to optimise that to a hash-based or index-based lookup) and [@]'s running time is proportional to the length of its first argument only - which here is always a singleton or empty list. The only extra cost is in space - you allocate a cons cell for each item which is included in the list twice (once to put it in the singleton list and once to append the rest of the list to it).

If the actual scenario is lots more of these put together, you could profitably define a better special-case operator:

let (@@) a b = match a with [] -> b | [a] -> a::b | _ -> assert false

(a dirty - though still safe - solution with Obj.magic can reduce the [match] to an [if]...)


David


  parent reply	other threads:[~2012-01-20  8:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-20  6:38 Martin DeMello
2012-01-20  6:46 ` Valentin ROBERT
2012-01-20  6:58   ` Martin DeMello
2012-01-20  8:37   ` David Allsopp [this message]
2012-01-20 13:29     ` Edgar Friendly
2012-01-20 13:50       ` Fabrice Le Fessant
2012-01-20 13:58       ` oliver
2012-01-20 14:05         ` Edgar Friendly
2012-01-20 14:12       ` David Allsopp
2012-01-20 14:23         ` Fabrice Le Fessant
2012-01-20 14:23         ` Edgar Friendly
2012-01-20  8:37   ` Sebastien Ferre
2012-01-20  9:11     ` Jerome Vouillon
2012-01-20  9:34       ` Fabrice Le Fessant
2012-01-20 10:27         ` Arnaud Spiwack
2012-01-20  8:52 ` Lin
2012-01-20  9:08   ` Valentin ROBERT
2012-01-20  9:19     ` Lin
2012-01-20 10:21     ` Martin DeMello
2012-01-20  9:38 ` oliver
2012-01-20 13:59   ` Edgar Friendly
2012-01-20 14:42     ` oliver
2012-01-20 15:31     ` Fabrice Le Fessant
2012-01-20 21:04     ` oliver
2012-01-20 21:09       ` oliver
2012-01-20 20:40 ` oliver
2012-01-20 21:07   ` Martin DeMello

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=E51C5B015DBD1348A1D85763337FB6D9C264BA49@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=martindemello@gmail.com \
    --cc=valentin.robert.42@gmail.com \
    /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).