Scheme Macro for nesting expressions

余生颓废 提交于 2019-12-05 10:38:27
(define bop list)

(define-syntax op
  (syntax-rules ()
    ((op a b) (bop a b))
    ((op a b c ...) (op (bop a b) c ...))))

For example, (op 1 2 3 4) expands to (bop (bop (bop 1 2) 3) 4) and evaluates to (((1 2) 3) 4).

The function you want to apply to the arguments should itself be an argument to the macro. Barring that, my solution was the same.

#!r6rs

(import (rnrs base))

(define-syntax claudiu
  (syntax-rules ()
    ((claudiu fun first second)
     (fun first second))
    ((claudiu fun first second rest ...)
     (claudiu fun (claudiu fun first second) rest ...))))

To show how the answer works out:

(op 1 2 3 4)

This is an op with 4 statements, so the 2nd case gets selected with a=1, b=2, c=3, ...=4:

(op (bop 1 2) 3 4)

This is an op with 3 statements, so 2nd case again. a=(bop 1 2), b=3, c=4:

(op (bop (bop 1 2) 3) 4)

Now this is a bop with 2 statements, so a=(bop (bop 1 2) 3), b=4, and it's done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!