From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 548ABBB84 for ; Thu, 29 Jun 2006 23:51:03 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k5TLp3jq015300 for ; Thu, 29 Jun 2006 23:51:03 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA14630 for ; Thu, 29 Jun 2006 23:51:02 +0200 (MET DST) Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.202]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k5TLp1tM015295 for ; Thu, 29 Jun 2006 23:51:02 +0200 Received: by nz-out-0102.google.com with SMTP id x7so22534nzc for ; Thu, 29 Jun 2006 14:51:01 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=kJCUK3wvWfEpTfxz86yW/vHnvDQj6e3LdXy0EDddOhLpY3kzVgLL79RKIwgfv2p8rEM5dk3ijR8UwoksXS2IKrsnhH9UorVV6U/b6e6fEDG3d2GlHGmjCmOTU0ysi+60b87DRN26Y/MXUCqV+9oYIFvJjR5VvAUOowKoa/umacQ= Received: by 10.36.140.3 with SMTP id n3mr3533317nzd; Thu, 29 Jun 2006 14:51:01 -0700 (PDT) Received: by 10.36.101.10 with HTTP; Thu, 29 Jun 2006 14:51:01 -0700 (PDT) Message-ID: Date: Fri, 30 Jun 2006 09:51:01 +1200 From: "Jonathan Roewen" To: "Richard Jones" Subject: Re: Another question on variant types and matching (was: Re: [Caml-list] Question on Variant Types) Cc: caml-list@inria.fr In-Reply-To: <20060629212717.GA32073@furbychan.cocan.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20060629212717.GA32073@furbychan.cocan.org> X-j-chkmail-Score: MSGID : 44A44B46.000 on concorde : j-chkmail score : X : 0/20 1 X-Miltered: at concorde with ID 44A44B47.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44A44B46.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; foreground:01 foreground:01 endline:01 endline:01 notepad:01 beginner's:01 ocaml:01 beginners:01 bug:01 wrote:01 caml-list:01 caml-list:01 pair:01 pair:01 bin:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 Hi, You just need some type constraints so it knows you're starting with a colour pair, not a colour' pair: let process_instructions (initial:colour * colour) = List.fold_left ( fun (fg, bg) -> function | Set_foreground `Default -> let new_fg = default_fg in (new_fg, bg) | Set_foreground (#colour as new_fg) -> (new_fg, bg) | Set_background `Default -> let new_bg = default_bg in (fg, new_bg) | Set_background (#colour as new_bg) -> (fg, new_bg) ) initial Jonathan On 6/30/06, Richard Jones wrote: > I have another question on variant types which seems to be related to > this, but perhaps the opposite way round. How can I get the code > below to work? (It's simplified greatly from a real problem I'm > having). > > Rich. > > ---------------------------------------------------------------------- > type colour = [ `Red | `Green | `Blue ] > type colour' = [ colour | `Default ] > type instructions = Set_foreground of colour' | Set_background of colour' > > let default_fg : colour = `Red > let default_bg : colour = `Green > > let process_instructions = > List.fold_left ( > fun (fg, bg) -> > function > | Set_foreground `Default -> > let new_fg = default_fg in > (new_fg, bg) > | Set_foreground new_fg -> > (new_fg, bg) > | Set_background `Default -> > let new_bg = default_bg in > (fg, new_bg) > | Set_background new_bg -> > (fg, new_bg) > ) > > let string_of_colour = function > | `Red -> "red" > | `Green -> "green" > | `Blue -> "blue" > > let () = > let instrs = > [ Set_foreground `Blue; Set_background `Red; Set_foreground `Default ] in > let fg, bg = `Green, `Blue in > let fg, bg = process_instructions (fg, bg) instrs in > print_endline ("fg = " ^ string_of_colour fg); > print_endline ("bg = " ^ string_of_colour bg) > ---------------------------------------------------------------------- > > -- > Richard Jones, CTO Merjis Ltd. > Merjis - web marketing and technology - http://merjis.com > Team Notepad - intranets and extranets for business - http://team-notepad.com > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >