From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 6F239BBC1 for ; Fri, 25 Apr 2008 10:22:41 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlcBAMQxEUhQRFuwiGdsb2JhbACRVgEBAQ8gmlg X-IronPort-AV: E=Sophos;i="4.25,706,1199660400"; d="scan'208";a="11913158" Received: from furbychan.cocan.org ([80.68.91.176]) by mail3-smtp-sop.national.inria.fr with ESMTP; 25 Apr 2008 10:22:40 +0200 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1JpJCY-00087w-He; Fri, 25 Apr 2008 09:22:38 +0100 Date: Fri, 25 Apr 2008 09:22:38 +0100 To: John Whitington Cc: David Teller , Caml Subject: Re: [Caml-list] [OSR] Standard syntax extensions ? Message-ID: <20080425082238.GA30805@annexia.org> References: <1209052182.6180.35.camel@Blefuscu> <9AA053A7-5131-437C-80DF-86B2497B0859@coherentgraphics.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9AA053A7-5131-437C-80DF-86B2497B0859@coherentgraphics.co.uk> User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Spam: no; 0.00; syntax:01 0100,:01 camlp:01 trivial:01 camlp:01 redistribute:01 ocamlfind:01 ocamlc:01 lib:01 -pp:01 cmo:01 ocamlfind:01 ocamlc:01 -pp:01 cmo:01 On Thu, Apr 24, 2008 at 04:52:50PM +0100, John Whitington wrote: > On 24 Apr 2008, at 16:49, David Teller wrote: > >* what kind of syntactic sugar is absolutely missing from the > >language ? > > I'd like a keyword "matches", so I can write > > map (matches (0, _, _)) l > > rather than > > map (function (0, _, _) -> true | _ -> false) l > > Had anyone done that or similar in camlp4? That's a good idea. Attached is a trivial camlp4 implementation which works to a first approximation. ('when'-clauses don't seem to work but no doubt would be very simple to fix/add). Rich. -------------------------------------------------- pa_matches.ml (* matches keyword. * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * $Id$ * * Compile with: * ocamlfind ocamlc -I +camlp4 camlp4lib.cma \ * -pp camlp4of.opt -c pa_matches.ml -o pa_matches.cmo * Test with: * ocamlfind ocamlc -pp "camlp4o pa_matches.cmo" test.ml -o test *) open Camlp4.PreCast open Syntax open Ast let output_matches _loc patt = <:expr< function $patt$ -> true | _ -> false >> EXTEND Gram GLOBAL: expr; expr: LEVEL ";" [ [ "matches"; p = patt -> output_matches _loc p ] ]; END -------------------------------------------------- test.ml open Printf let () = let xs = List.filter (matches 7) [ 1; 2; 3; 4; 5; 6; 7; 7; 7; 8; 9; 10 ] in printf "result = %s\n" (String.concat ";" (List.map string_of_int xs)) -------------------------------------------------- -- Richard Jones Red Hat