Re: [racket-users] Macro help

2019-05-23 Thread Michael Murdock MacLeod
Does this work? It uses a helper function, `prune`, to parse the var-val 
clauses.

#lang racket

(define-for-syntax (prune stx)
  (syntax-case stx ()
[()
 #'()]
[((var val) others ...)
 (cons #'(var val)
   (prune #'(others ...)))]
[(var others ...)
 (cons #'(var #f)
   (prune #'(others ...)))]))

(define-syntax (aux stx)
  (syntax-case stx ()
[(_ terms ...)
 (with-syntax ([((var val) ...) (prune #'(terms ...))])
   #'(define-values (var ...) (values val ...)))]))

(aux a (b (* 2 pi)) c (d pi))
a
b
c
d

;; output shown below

#f
6.283185307179586
#f
3.141592653589793

On Thursday, May 23, 2019 9:41:17 PM PDT Kevin Forchione wrote:
> Hi guys,
> I’ve been wracking my brains all day trying to come up with a macro that
> would convert this syntax:
> 
> ;; (aux a (b (* 2 pi)) c (d pi))
> ;; => (define-values (a b c d) (values #f 6.28318530717958 #f
> 3.141592653589793)
> 
> 
> I’m missing some part of the picture. The closest I’ve come is to create a
> list of the pairs:
> 
> #lang racket
> 
> (define-syntax (aux stx)
>   (syntax-case stx ()
> [(_ (var val)) #'`((var ,val))]
> [(_ var) #''((var #f))]
> [(_ (var0 val0) var1 ...) #'(append `((var0 ,val0)) (aux var1 ...))]
> [(_ var0 var1 ...) #'(append '((var0 #f)) (aux var1 ...))]))
> 
> (aux a (b (* 2 pi)) c (d pi)) ;=> '((a #f) (b 6.283185307179586) (c #f) (d
> 3.141592653589793))
> 
> 
> Any help is greatly appreciated!
> 
> Kevin


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/2893163.LJ05K77S5N%40alphtsr.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] question about matching with "(? ...)"

2019-04-21 Thread Michael Murdock MacLeod
Match treats `empty` as an identifier to bind, not as a pattern for the empty 
list. This means that the first case will always be matched. You can use 
`(list)` or `'()` to match an empty list.

Also, in the last catch-all case, you pass the function `rest` which returns 
the tail of a list to `filter-odds`. You probably wanted to shadow that binding 
by using `(cons first rest)` instead of `_`.

Here's the version with the fixes above:

(define (filter-odds v)
  (match v
[(list) (list)]
[(cons (? odd? first) rest)
 (cons first (filter-odds rest))]
[(cons first rest) (filter-odds rest)]))

(filter-odds '(1 2 3 4 5)) ; ==> '(1 3 5)

Best,
Michael MacLeod

On Sunday, April 21, 2019 1:19:03 PM PDT Tim Meehan wrote:
> Forgive this naive question, I am having some trouble understanding some
> matching forms. If I wanted to filter out odd numbers like this:
> 
> (filter odd? '(1 2 3 4 5)) => '(1 3 5)
> 
> but I wanted to do this with a match:
> (define (filter-odds v)
>   (match v
> [empty empty]
> [(cons (? odd? first) rest) (cons first (filter-odds rest))]
> [_ (filter-odds rest)]))
> 
> (filter-odds '(1 2 3 4 5)) => '(1 2 3 4 5)


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] catch and bind an unbound id in a macro

2019-04-19 Thread Michael Murdock MacLeod
I'm in no ways a macro expert, but will this work? It uses identifier-binding 
to check if the identifier for the hash table is bound.

(define-syntax (set/define stx)
  (syntax-case stx ()
[(_ ht key value)
 (cond [(identifier-binding #'ht)
#'(hash-set! ht key value)]
   [else
#'(begin
(define ht (make-hash))
(hash-set! ht key value))])]))

On Friday, April 19, 2019 2:33:54 PM PDT zeRusski wrote:
> Not quite what I had in mind. The following examples must all work without
> errors:
> 
> #lang racket
> 
> ;; set/define defined here
> 
> (set/define h a 1)
> (set/define h a 2)
> (hash-set! h 'b 42)
> 
> (define bar (make-hash))
> (set/define bar a 1)
> 
> (define (foo)
>   (set/define local-h a 1)
>   (set/define local-h a 2)
>   (hash-set! local-h 'b 42)
>   local-h)
> 
> (foo)
> 
> Basically, if the identifier h is unbound at runtime
>   (set/define h key val)
> is the same as
>  (define h (make-hash))
>  (hash-set! h 'key val)
> 
> If h is bound
>  (hash-set! h 'key val)


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help understanding cond expression

2019-01-15 Thread Michael Murdock MacLeod
On Saturday, January 12, 2019 8:34:35 PM PST Hassan Shahin wrote:
> I have this definition for a procedure:
> 
> (define type-of (lambda (item)
>  (cond
>[(pair? item) 'pair]
>[(null? item) 'empty-list]
>[(number? item) 'number]
>[(symbol? item) 'symbol]
>[else 'some-other-type])))
> 
> My understanding is that if the first 4 conditions fail (=> #f
> ), then the last expression (the else
> expression) is evaluated.
> When I apply this procedure to John, as in (type-of John) I get an error
> (; john: undefined; ; cannot reference an identifier before its definition)
> .
> 
> What is going on?
> Thanks

The issue is that John is evaluated before it is passed to type-of. For 
example, the expression (type-of (+ 2 3)) is equivalent to (type-of 5), 
because the expression (+ 2 3) is evaluated to 5 before it is supplied to the 
type-of function.

In the Google+ post you write "cond behaves as if (not evaluating its 
arguments)". It is true that the arguments to cond, i.e. [(pair? item) 'pair], 
[(null? item) 'empty-list], and so on are not immediately evaluated. However, 
item is evaluated before it is passed to type-of, hence the "cannot reference" 
error.

See https://docs.racket-lang.org/reference/if.html and 
https://docs.racket-lang.org/reference/eval-model.html?q=evaluation%20model for 
more information 
about how Racket evaluates expressions.


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.