0

我正在编写一些获取设定长度列表的诡计代码,我需要为列表中的每个元素定义一个变量。目前,我必须做这样的事情:

(define (foo l)
  (let ((e-1 (car l))
        (e-2 (cadr l))
        (e-2 (caddr l))
        ; ...
        (e-n (list-ref (- n 1)
                       l)))
    (compute)))

这变得超级乏味。无论如何我可以做这样的事情吗?

(define (foo l)
  (symbol-def e-1 e-2 e-3 e-4 e-n l)
  (compute))

编辑:使问题更加狡猾。

4

1 回答 1

0

Guile-specific,我发现了ice-9 匹配模块,它具有以下形式:

(match lst
    ((pattern) expr))

例子:

(use-modules (ice-9 match))

(let ((l '(test foo bar)))
  (match l
    ((head second third)
     second)))

; returns `foo`
于 2014-01-24T02:27:41.340 回答