From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q0K8bJaf023463 for ; Fri, 20 Jan 2012 09:37:19 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkACABQnGU9RZ90vlGdsb2JhbABDhQSoDWoiAQEBAQkLCQkUAyKBcgEBAQQjBA1FEAIBCBgCAgYgAgICMBUQAgQBDQ0Th2OnD5FjgS+JYTNjBJUYkks X-IronPort-AV: E=Sophos;i="4.71,541,1320620400"; d="scan'208";a="140680693" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail1-smtp-roc.national.inria.fr with ESMTP; 20 Jan 2012 09:37:14 +0100 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20120120083713.QBQB9217.mtaout01-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com>; Fri, 20 Jan 2012 08:37:13 +0000 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout02-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20120120083713.OIHG5924.aamtaout02-winn.ispmail.ntl.com@romulus.metastack.com>; Fri, 20 Jan 2012 08:37:13 +0000 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id q0K8b9wD025611 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Fri, 20 Jan 2012 08:37:09 GMT Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%10]) with mapi id 14.01.0355.002; Fri, 20 Jan 2012 08:37:09 +0000 From: David Allsopp To: Valentin ROBERT , Martin DeMello CC: OCaml List Thread-Topic: [Caml-list] is there a more concise way to write this? Thread-Index: AQHM1z46do/3DbmtDEuU2RAHLw8lP5YU0B8AgAAaE6A= Date: Fri, 20 Jan 2012 08:37:08 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.0.7] Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=R50lirqlHffDPPkwUlkuVa99MrvKdVWo//yz83qex8g= c=1 sm=0 a=40hZHaDki8MA:10 a=QlCY4dNUgAMA:10 a=cTs9vV391PwA:10 a=IkcTkHD0fZMA:10 a=xqWC_Br6kY4A:10 a=ALDqTLFfLPXr17I2WqsA:9 a=QEXdDO2ut3YA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by walapai.inria.fr id q0K8bJaf023463 Subject: RE: [Caml-list] is there a more concise way to write this? 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