[plt-scheme] question about rnrs/enums-6

From: Thomas Chust (chust at web.de)
Date: Sun Jan 18 09:19:25 EST 2009

Luka Stojanovic wrote:
> [...]
>   #lang/scheme
>   (require rnrs/enums-6)
> 
>   (define my-enum1 (make-enumeration '(a b c)))
>   ; will cause: make-enumeration: expected argument of type <list of symbols>; given (a b c)
> [...]

Hello,

this is another case where one is unexpectedly bitten by PLT Scheme's
recent change to immutable pairs (and hence lists) in the default Scheme
dialect while the RnRS dialects continue to use mutable pairs (and hence
lists).

To make your code work, you have to pass a mutable list to
make-enumeration instead of an immutable one. For example you can do the
following:

  #lang scheme
  (require
   scheme/mpair
   rnrs/enums-6)

  (define my-enum1
    (make-enumeration (mlist 'a 'b 'c)))

> [...]
> What would be "the proper", most idiomatic way to handle this situation anyway?
> [...]

Scheme is a weakly typed language, so using symbols instead of instances
of some special enumerated type is stylistically ok, I think. You can
find many places where this is done -- for example in keyword arguments
to the PLT standard library functions opening files.

However it is practical to be able to check whether the symbols belong
to a certain predefined set. In the default dialect of PLT Scheme I
would recommend using contracts for this purpose, like this:

  (define (my-function sym)
    (case sym
      [(symbol-a)
       ... do something ...]
      [(symbol-b)
       ... do something ...]
      [(symbol-c)
       ... do something ...]))

  (provide/contract
   [my-function ((one-of/c 'symbol-a 'symbol-b 'symbol-c) . -> . any)])

To check the membership of a symbol in a set yourself, I would recommend
to use hash tables:

  (define my-symbol-set
    #hasheq((symbol-a . #t) (symbol-b . #t) (symbol-c . #t)))

  (define (in-my-symbol-set? sym)
    (hash-ref my-symbol-set sym #f))

If you need or wish to use full blown enumerated types with many more
features than the R6RS enumerations, there is a PLaneT package providing
you with the necessary tools:

  (require (planet dvanhorn/finite-types:1:1/finite-types))

  (define-enumerated-type color :color
    color?
    colors
    color-name
    color-index
    (red green blue black white))

I hope this helps you solve your problem :-)

cu,
Thomas


Posted on the users mailing list.