Turning a list value in a parameter into a list for eval without quasiquote/unquote 
Author Message
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

There has to be a better way to do this.

 I'm trying to create an enumeration type module. My code (which seems
to work) looks like this:

(define (define-enum name vals)
  (define (make-enum-crt name vals)
    (list 'define (list name 'val)
      (list 'cond (list (list 'member 'val
        (quasiquote (car (list (quote (unquote vals))))))
          (list 'list (quasiquote (quote (unquote name))) 'val))
          (list 'else (list 'error "Invalid Enum Value"))))
  )
  (eval (make-enum-crt name vals))
)

   Breaking it up (ie. if I define the procedure in
user-initial-environment), (make-enum-crt 'fred (list "rock"
"hammer")) is

scheme> (make-enum-crt 'fred (list "rock" "stone"))

(define (fred val) (cond ((member val (car (list (quote ("rock"
"stone"))))) (li
st (quote fred) val)) (else (error "Invalid Enum Value"))))
scheme> (eval (make-enum-crt 'fred (list "rock" "stone")))

#unspecified
scheme> (fred "rock")

(fred "rock")
scheme>

   Good oh. But *oh boy* is it ugly.

   In the opinion of the assembled gurus (before whom I know prostrate
my pathetic and unworthy self) is there another way of dealing with
this that avoid all of that (quasiquote/unquote) stuff? (I know about
the syntactical short-cuts, but I'm looking for something more
creative).

    KR

                Pb



Sun, 15 Aug 2004 07:24:26 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

    Paul> There has to be a better way to do this.
    Paul>  I'm trying to create an enumeration type module. My code
    Paul>  (which seems
    Paul> to work) looks like this:

    Paul> (define (define-enum name vals)
    Paul>   (define (make-enum-crt name vals)
    Paul>     (list 'define (list name 'val)
    Paul>       (list 'cond (list (list 'member 'val
    Paul>         (quasiquote (car (list (quote (unquote vals))))))
    Paul>           (list 'list (quasiquote (quote (unquote name)))
    Paul>           'val)) (list 'else (list 'error "Invalid Enum
    Paul>           Value"))))
    Paul>   ) (eval (make-enum-crt name vals))
    Paul> )

This is better done as a macro:

(define-macro (define-enum name vals)
  `(define (,name val)
     (or (member val ,vals)
         (error "Invalid Enum Value"))))

(define-enum fred '("rock" "hammer"))

But this is just a set, and there are plenty of set handling procedures.
If you're looking for actual "enumerated" values, you might want to
check out some C FFI/wrapper generators which usually handle C enums.
SWIG and gwrap both do this.

--
Alex



Sun, 15 Aug 2004 09:14:23 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

Quote:
> There has to be a better way to do this.

(define (make-enum-crt name vals)
  (lambda (val)
    (cond ((member val vals) (cons name val))
           (else (error "Invalid Enum Value")))))

(define-syntax define-enum
  (syntax-rules ()
    ((define-enum name vals) (define name (make-enum-crt 'name 'vals)))))

(define-enum fred ("rock" "stone"))

I suggest you learn more about lambda before using eval.  Most of the
time, if you think lambda can't do it, you just don't know lambda.

-al



Sun, 15 Aug 2004 09:31:28 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

Quote:
>  I'm trying to create an enumeration type module.

Is there some reason why the enums have to be strings?  Symbols seem
more amenable to this purpose.

Will the enums be constants known at compile time (e.g. '(rock stone)),
or will you read them from some external source at run time?  If they're
known at compile time there are more optimizations you can do.



Sun, 15 Aug 2004 22:35:18 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

Quote:
> There has to be a better way to do this.

  And three of you thoughtfully replied:

Alex Shinn:

(define-macro ...)

   Good alternative. And the macro angle is what I am trying to come
at.

   And it isn't that the enums are sets. (list "rock" "hammer") is
merely the list of valid values for the enum. I'm doing this as part
of a "learn Scheme" exercise, so I'd rather not use the 'C' wrapper
approach. Maybe later.

 Al Petrofsky:

"Most of the time, if you think lambda can't do it, you just don't
know lambda."

  This is just what I was looking for (I think: I'll have a play with
it). And an aphorism to live by, for nothing.b

 Bruce Lewis:

  The enums don't have to be strings:

(fred (list "rock" "hammer" 12 'hello-world fred))

  but the values may not be known until run-time (which is why I'm
taking this approach).

  - Thanks to all of you.

   KR

            PB



Mon, 16 Aug 2004 02:44:29 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote



Quote:
> There has to be a better way to do this.

>  I'm trying to create an enumeration type module. My code (which seems
> to work) looks like this:

> (define (define-enum name vals)
>   (define (make-enum-crt name vals)
>     (list 'define (list name 'val)
>       (list 'cond (list (list 'member 'val
>         (quasiquote (car (list (quote (unquote vals))))))
>           (list 'list (quasiquote (quote (unquote name))) 'val))
>           (list 'else (list 'error "Invalid Enum Value"))))
>   )
>   (eval (make-enum-crt name vals))
> )

>    Breaking it up (ie. if I define the procedure in
> user-initial-environment), (make-enum-crt 'fred (list "rock"
> "hammer")) is

> scheme> (make-enum-crt 'fred (list "rock" "stone"))

> (define (fred val) (cond ((member val (car (list (quote ("rock"
> "stone"))))) (li
> st (quote fred) val)) (else (error "Invalid Enum Value"))))
> scheme> (eval (make-enum-crt 'fred (list "rock" "stone")))

> #unspecified
> scheme> (fred "rock")

> (fred "rock")
> scheme>

>    Good oh. But *oh boy* is it ugly.

>    In the opinion of the assembled gurus (before whom I know prostrate
> my pathetic and unworthy self) is there another way of dealing with
> this that avoid all of that (quasiquote/unquote) stuff? (I know about
> the syntactical short-cuts, but I'm looking for something more
> creative).

>     KR

>                 Pb

If you don't want to use macro and quasiquote you can also do that:
(define (define-enum name vals)
  (define (make-enum-crt name vals)
    (list 'define (list name 'val)
          (list 'cond (list (list 'member 'val
                                  (list 'quote vals))
                            (list 'list (list 'quote name) 'val))
                (list 'else (list 'error "Invalid Enum Value")))))
  (eval (make-enum-crt name vals))
  )

But beware eval is evil :)



Mon, 16 Aug 2004 08:56:01 GMT  
 Turning a list value in a parameter into a list for eval without quasiquote/unquote

    Paul> And it isn't that the enums are sets. (list "rock" "hammer")
    Paul> is merely the list of valid values for the enum. I'm doing
    Paul> this as part of a "learn Scheme" exercise, so I'd rather not
    Paul> use the 'C' wrapper approach. Maybe later.

Sorry, I should have clarified, I wasn't suggesting you use a C wrapper,
but rather the ideas they use to implement enums.  Like storing enum
lookup tables in hashes and/or arrays to quickly go to/from the name and
value of the enum.  A hash table gives you quick lookup, but takes more
space, and you need two tables to go in both directions.  If you keep a
sorted array, you can do a binary search.  And if the enumerated values
are integers within a narrow range, you're fastest approach for the
reverse lookup (from value to name) is an array where the index is the
value.

If there *isn't* an associated value (as in the examples), then the list
is in fact not enumerated, and you really are talking about a set.

--
Alex



Mon, 16 Aug 2004 09:05:50 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How do I turn values() into a list?

2. Non-intuitive treatment of empty list as default parameter value on class method

3. Removing the values from a list from another list

4. Ada 0y wish list: parameters of package parameters

5. Intersection of multiple lists/list of lists

6. Multiple-value-list with no values.

7. DXOracle / PyADO - convert lists of tuples to list of lists

8. CW55 turn off list control marking

9. LOGO-L> (turn a list);butitem

10. Why quasiquote without macros?

11. Read a line from a file, turn into a list of numbers

12. fast way of turning a list of integers<256 into a string

 

 
Powered by phpBB® Forum Software