【问题标题】:Can I assign a list of symbols to a list of elements in Guile?我可以将符号列表分配给 Guile 中的元素列表吗?
【发布时间】:2014-02-14 21:03:40
【问题描述】:

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

(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))

编辑:使问题更加狡猾。

【问题讨论】:

标签: scheme guile


【解决方案1】:

Guile 特有的,我找到了ice-9 match module,它的格式如下:

(match lst
    ((pattern) expr))

例子:

(use-modules (ice-9 match))

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

; returns `foo`

【讨论】:

  • 它不是特别针对 guile,因为 guile 使用 Alex Shinn 的可移植匹配实现。
猜你喜欢
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2016-04-21
  • 2021-07-04
  • 2012-01-05
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多