有什么方法可以将 (list 1 2 3) 转换为 Racket(Scheme) 中的 1 2 3 吗?
Is there any way to convert (list 1 2 3) to just 1 2 3 in Racket(Scheme)?
#lang racket
(require syntax/parse/define)
(require racket/draw)
(define thickness 1.99)
(define color-set
(hash
`black `(0 0 0)
`white `(255 255 255)
`light-grey `(229 229 229)
`dark-grey `(153 153 153)))
(define font-set
(hash
`default (make-font #:size 17 #:family 'script #:weight '550 #:smoothing `smoothed)
`index (make-font #:size 11 #:family 'modern #:weight '400 #:smoothing `smoothed)
`comment (make-font #:size 11 #:family 'modern #:weight '400 #:smoothing `smoothed)))
(define-syntax-parser grab
#:datum-literals (pen brush font)
[(_ pen color alpha)
(send dc set-pen (make-color ,@(hash-ref color-set (quasiquote color)) alpha))]
[(_ pen color)
(send dc set-pen (make-color ,@(hash-ref color-set (quasiquote color)) 1))]
[(_ brush color style)
(send dc set-brush (make-color ,@(hash-ref color-set (quasiquote color)) alpha) (quasiquote style))]
[(_ brush color)
(send dc set-brush (make-color ,@(hash-ref color-set (quasiquote color)) alpha) 'solid)]
[(_ font mode)
(send dc set-font (hash-ref font-set (quasiquote mode)))])
这个问题是关于基本球拍宏的。
我正在用 racket/draw 制作一些东西。
我想制作自己的抓取功能,使设置笔、设置字体、设置画笔更直观。
但是我遇到了一个问题。
我想做的是,
(make-color ,@(hash-ref color-set (quasiquote color)) 1)
,
我想创建 (make-color 255 255 255 1)
。 (如果颜色是黑色)
但我收到错误消息:
unquote-splicing: not in quasiquote in: (unquote-splicing (hash-ref color-set (quasiquote color)))
好像,@
只能用来(list 1 @,(list 1 2 3) 2 3)
。不适用于 (list 255 255 255)
到 255 255 255
.
我能做什么?谢谢。
对于初学者来说,大多数时候您应该使用引号,而不是准引号。例如:
(define color-set
(hash
'black '(0 0 0)
'white '(255 255 255)
'light-grey '(229 229 229)
'dark-grey '(153 153 153)))
不用准引用就可以做你想做的事情很简单,我们只需要根据需要构建参数列表和apply
过程:
(apply make-color (append (hash-ref color-set color) (list 1)))
原则上我们可以使用准引用和列表拼接来完成你想要的,但是我们必须记住,当我们对一个列表进行准引用时,我们正在处理一个列表符号,它们需要被评估为有意义的,这就是事情变得丑陋的时候:
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(eval `(make-color ,@(hash-ref color-set color) 1) ns)
#lang racket
(require syntax/parse/define)
(require racket/draw)
(define thickness 1.99)
(define color-set
(hash
`black `(0 0 0)
`white `(255 255 255)
`light-grey `(229 229 229)
`dark-grey `(153 153 153)))
(define font-set
(hash
`default (make-font #:size 17 #:family 'script #:weight '550 #:smoothing `smoothed)
`index (make-font #:size 11 #:family 'modern #:weight '400 #:smoothing `smoothed)
`comment (make-font #:size 11 #:family 'modern #:weight '400 #:smoothing `smoothed)))
(define-syntax-parser grab
#:datum-literals (pen brush font)
[(_ pen color alpha)
(send dc set-pen (make-color ,@(hash-ref color-set (quasiquote color)) alpha))]
[(_ pen color)
(send dc set-pen (make-color ,@(hash-ref color-set (quasiquote color)) 1))]
[(_ brush color style)
(send dc set-brush (make-color ,@(hash-ref color-set (quasiquote color)) alpha) (quasiquote style))]
[(_ brush color)
(send dc set-brush (make-color ,@(hash-ref color-set (quasiquote color)) alpha) 'solid)]
[(_ font mode)
(send dc set-font (hash-ref font-set (quasiquote mode)))])
这个问题是关于基本球拍宏的。 我正在用 racket/draw 制作一些东西。 我想制作自己的抓取功能,使设置笔、设置字体、设置画笔更直观。 但是我遇到了一个问题。
我想做的是,
(make-color ,@(hash-ref color-set (quasiquote color)) 1)
,
我想创建 (make-color 255 255 255 1)
。 (如果颜色是黑色)
但我收到错误消息:
unquote-splicing: not in quasiquote in: (unquote-splicing (hash-ref color-set (quasiquote color)))
好像,@
只能用来(list 1 @,(list 1 2 3) 2 3)
。不适用于 (list 255 255 255)
到 255 255 255
.
我能做什么?谢谢。
对于初学者来说,大多数时候您应该使用引号,而不是准引号。例如:
(define color-set
(hash
'black '(0 0 0)
'white '(255 255 255)
'light-grey '(229 229 229)
'dark-grey '(153 153 153)))
不用准引用就可以做你想做的事情很简单,我们只需要根据需要构建参数列表和apply
过程:
(apply make-color (append (hash-ref color-set color) (list 1)))
原则上我们可以使用准引用和列表拼接来完成你想要的,但是我们必须记住,当我们对一个列表进行准引用时,我们正在处理一个列表符号,它们需要被评估为有意义的,这就是事情变得丑陋的时候:
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(eval `(make-color ,@(hash-ref color-set color) 1) ns)