OpenMusic:使用 lisp 生成 L-System 树
OpenMusic: L-System tree generation using lisp
我正在尝试使用一个名为 OpenMusic 的组合工具,它是一个基于通用 lisp 的图形开发环境,它使用了一种叫做 "rhythm trees" 的东西。我正在尝试使用一组规则创建节奏树,在 OM 中,这棵树必须具有以下结构:
- 树是包含两个元素的列表
- 它的第一个元素是一个节点
- 它的第二个元素是它的子元素列表(也可以是树)
给定树深度n
,初始单节点树(1)
和转换规则:
- (1) -> (1 2)
- (2) -> (1)
它应该给出:
n = 1 -> (1 (1 2))
trees/lists 的递归算法通常用 COND
编写。你弄清楚结束条件是什么,然后把它们放在第一位。然后处理原子,最后通过递归调用其 CAR
和 CDR
上的函数来遍历列表,并 CONS
结果生成输出列表。所以基本模板是这样的:
(defun algo (list)
(cond
((end-condition) suitable-end-value)
((atom list) (do-something-with-atom list))
(t (cons (algo (car list)
(algo (cdr list))))))
在这种情况下,这将类似于:
(defun rhythm-tree (n tree)
(cond ((zerop n) tree) ; First end-condition.
((null tree) '()) ; Another end-condition.
((atom tree) ; An atom: transform into...
(cons tree ; a list of the atom itself...
(list (rhythm-tree (1- n) ; and a list of children.
(case tree
(1 (list 1 2))
(2 (list 1))
(otherwise '()))))))
;; Iterate over elements of a list.
(t (cons (rhythm-tree n (car tree))
(rhythm-tree n (cdr tree))))))
(rhythm-tree 1 1)
;=> (1 (1 2))
(rhythm-tree 2 1)
;=> (1 ((1 (1 2)) (2 (1))))
我正在尝试使用一个名为 OpenMusic 的组合工具,它是一个基于通用 lisp 的图形开发环境,它使用了一种叫做 "rhythm trees" 的东西。我正在尝试使用一组规则创建节奏树,在 OM 中,这棵树必须具有以下结构:
- 树是包含两个元素的列表
- 它的第一个元素是一个节点
- 它的第二个元素是它的子元素列表(也可以是树)
给定树深度n
,初始单节点树(1)
和转换规则:
- (1) -> (1 2)
- (2) -> (1)
它应该给出:
n = 1 -> (1 (1 2))
trees/lists 的递归算法通常用 COND
编写。你弄清楚结束条件是什么,然后把它们放在第一位。然后处理原子,最后通过递归调用其 CAR
和 CDR
上的函数来遍历列表,并 CONS
结果生成输出列表。所以基本模板是这样的:
(defun algo (list)
(cond
((end-condition) suitable-end-value)
((atom list) (do-something-with-atom list))
(t (cons (algo (car list)
(algo (cdr list))))))
在这种情况下,这将类似于:
(defun rhythm-tree (n tree)
(cond ((zerop n) tree) ; First end-condition.
((null tree) '()) ; Another end-condition.
((atom tree) ; An atom: transform into...
(cons tree ; a list of the atom itself...
(list (rhythm-tree (1- n) ; and a list of children.
(case tree
(1 (list 1 2))
(2 (list 1))
(otherwise '()))))))
;; Iterate over elements of a list.
(t (cons (rhythm-tree n (car tree))
(rhythm-tree n (cdr tree))))))
(rhythm-tree 1 1)
;=> (1 (1 2))
(rhythm-tree 2 1)
;=> (1 ((1 (1 2)) (2 (1))))