caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: "Richard Jones" <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: Another question on variant types and matching (was: Re: [Caml-list] Question on Variant Types)
Date: Fri, 30 Jun 2006 09:51:01 +1200	[thread overview]
Message-ID: <ad8cfe7e0606291451neeb7a9cvb030f33a0b982060@mail.gmail.com> (raw)
In-Reply-To: <20060629212717.GA32073@furbychan.cocan.org>

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 <rich@annexia.org> 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
>


      reply	other threads:[~2006-06-29 21:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-28 16:27 Question on Variant Types Seth J. Fogarty
2006-06-28 16:47 ` [Caml-list] " Jonathan Roewen
2006-06-28 16:48   ` Jonathan Roewen
2006-06-28 23:44     ` Seth J. Fogarty
2006-06-28 23:51       ` Jonathan Roewen
     [not found]         ` <c7ee61120606281739g27fc344bt855cde4bd6a89797@mail.gmail.com>
2006-06-29  3:09           ` Jonathan Roewen
2006-06-29  3:37           ` Jacques Garrigue
2006-06-29 21:27 ` Another question on variant types and matching (was: Re: [Caml-list] Question on Variant Types) Richard Jones
2006-06-29 21:51   ` Jonathan Roewen [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=ad8cfe7e0606291451neeb7a9cvb030f33a0b982060@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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).