Scheme 方案程序参数

Scheme 方案程序参数,scheme,racket,Scheme,Racket,我正在创建一个函数 (define (run program-string . arguments) (if (string? program-string) (value-of (create-ast program-string) (empty-env)) (raise (string-append "expected a program as string, got: " (~a program-string)))

我正在创建一个函数

 (define (run program-string . arguments)
      (if (string? program-string)
          (value-of (create-ast program-string) (empty-env))
          (raise (string-append "expected a program as string, got: " (~a program-string)))
          )  
      )
其中“arguments”将是表示程序参数的参数字符串或空列表'()。“arguments”中的每个参数都将按位置绑定到程序字符串中的一个变量,即argN,其中N是特定参数的位置(从0开始)

因此,假设所有这些函数和值都已在我的语言中定义,并且xcord和ycord是最终移动后的实际坐标。给定该输入,我想将“up(3)”绑定到(0),将“right(5)”绑定到arg0。。。。等等,给定任意数量的argN和每个arg的相应输入量


run函数基本上需要为每个arg分配相应的参数并运行最后的字符串。

我试图准备一个适合这个问题的答案

我将假设所有像
up(3)
这样的东西都应该是像
(up 3)
这样的s表达式。如果不是,那就太糟糕了

你没有说你的例子的期望输出是什么。我希望我的数学是好的,并规定它的立场(14-7)

这个问题似乎假定只有以
(move(0)…
开头的程序才是合法的。我抓住了这个假设;我不会放手的

在这前奏曲中,我介绍了我对家庭作业练习的理解:

这个可怕的小可爱的孩子是
printf
eval

#lang racket

;; the current position and functions to move it
(struct pos (x y) #:transparent)
(define the-pos (make-parameter (pos 0 0)))

(define (dx f n)
  (match (the-pos)
    [(pos x y) (the-pos (pos (f x n) y))]))
(define (dy f n)
  (match (the-pos)
    [(pos x y) (the-pos (pos x (f y n)))]))

(define (left n)  (dx - n))
(define (right n) (dx + n))
(define (up n)    (dy - n))
(define (down n)  (dy + n))

;; namespace for `eval`
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))

;; start holding your nose
(define (run str . args)
  (define as (for/list ([a args])
               (read (open-input-string a))))
    (match (read (open-input-string str))
      [`(move (0 0) ,xs ...)
       (for ([x xs])
         (define n (match (symbol->string x)
                     [(pregexp "^arg(\\d)$" (list _ n)) (string->number n)]
                     [else (error 'run "bad argN")]))
         (unless (< -1 n (length as))
           (error 'run "bad argN"))
         (eval (list-ref as n) ns))
       (eval '(the-pos) ns)]))

;; a unit test to confirm our true evi^Hal
(require rackunit)
(the-pos (pos 0 0))
(check-equal?
 (run "(move (0 0) arg0 arg1 arg2 arg3 arg4 arg5 arg6)"
      "(up 3)"
      "(right 5)"
      "(right 4)"
      "(down 2)"
      "(up 6)"
      "(right 2)"
      "(right 3)")
 (pos 14 -7))

;; and since we went through all the argN nonsense, the reverse:
(the-pos (pos 0 0))
(check-equal?
 (run "(move (0 0) arg6 arg5 arg4 arg3 arg2 arg1 arg0)"
      "(right 3)"
      "(right 2)"
      "(up 6)"
      "(down 2)"
      "(right 4)"
      "(right 5)"
      "(up 3)")
 (pos 14 -7))
;; not that it matters with addition and subtraction...why am I even...
#朗球拍
;; 当前位置和移动它的功能
(结构位置(x y)#:透明)
(定义位置(生成参数(位置0)))
(定义(dx f n)
(匹配(pos)
[(pos x y)(pos(pos(f x n)y))])
(定义(dyfn)
(匹配(pos)
[(位置x y)(位置x(f y n))])
(定义(左n)(dx-n))
(定义(右n)(dx+n))
(定义(向上n)(dy-n))
(定义(向下n)(dy+n))
;; `eval'的命名空间`
(定义名称空间锚点a)
(定义ns(名称空间定位->名称空间a))
;; 开始捂住你的鼻子
(定义(运行str.args)
(定义为(for/list([a args]))
(读取(打开输入字符串a)))
(匹配(读取(打开输入字符串str))
[`(移动(0),xs…)
(对于([x xs])
(定义n(匹配(符号->字符串x)
[(pregexp“^arg(\\d)$”(列表n))(字符串->数字n)]
[其他(错误'运行“错误参数”)])
(除非(<-1N(长度为))
(错误“运行”错误参数)
(评估(列表参考号为n)ns)
(评估(pos)ns)])
;; 单元测试以确认我们的真实evi^Hal
(需要机架单元)
(位置(位置0)
(检查是否相等?
(运行)(移动(0)arg0 arg1 arg2 arg3 arg4 arg5 arg6)
(上升3)
“(权利5)”
“(权利4)”
(下跌2)
(上升6)
“(权利2)”
“(权利3)”
(位置14-7))
;; 因为我们经历了所有argN的胡说八道,反过来说:
(位置(位置0)
(检查是否相等?
(运行)(移动(0)arg6 arg5 arg4 arg3 arg2 arg1 arg0)
“(权利3)”
“(权利2)”
(上升6)
(下跌2)
“(权利4)”
“(权利5)”
(上升3)
(位置14-7))
;; 这与加减法无关…为什么我甚至。。。

你是想说
如果参数是list,那么就这样做,如果string是list,那么就这样做
还是说你不知道如何在定义中指定(从语法上讲)参数,比如:
(define(foo-bar)(display(string-append-bar“is-a-arg!!”)注意:你可能想考虑修改你的问题,因为你很难理解你想要做什么;尝试添加所需的代码使用量和预期输出,或某事……我不知道,但我不完全确定你在寻求什么帮助。我也很困惑你的问题。“参数将是一个参数字符串或一个空列表'()”,您不能同时拥有它。正如声明的那样,
参数
将是
列表
,而不是
字符串
。它可能是
列表
字符串
,但弄清楚您想要什么很重要。我已经编辑了它,我道歉。
#lang racket

;; the current position and functions to move it
(struct pos (x y) #:transparent)
(define the-pos (make-parameter (pos 0 0)))

(define (dx f n)
  (match (the-pos)
    [(pos x y) (the-pos (pos (f x n) y))]))
(define (dy f n)
  (match (the-pos)
    [(pos x y) (the-pos (pos x (f y n)))]))

(define (left n)  (dx - n))
(define (right n) (dx + n))
(define (up n)    (dy - n))
(define (down n)  (dy + n))

;; namespace for `eval`
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))

;; start holding your nose
(define (run str . args)
  (define as (for/list ([a args])
               (read (open-input-string a))))
    (match (read (open-input-string str))
      [`(move (0 0) ,xs ...)
       (for ([x xs])
         (define n (match (symbol->string x)
                     [(pregexp "^arg(\\d)$" (list _ n)) (string->number n)]
                     [else (error 'run "bad argN")]))
         (unless (< -1 n (length as))
           (error 'run "bad argN"))
         (eval (list-ref as n) ns))
       (eval '(the-pos) ns)]))

;; a unit test to confirm our true evi^Hal
(require rackunit)
(the-pos (pos 0 0))
(check-equal?
 (run "(move (0 0) arg0 arg1 arg2 arg3 arg4 arg5 arg6)"
      "(up 3)"
      "(right 5)"
      "(right 4)"
      "(down 2)"
      "(up 6)"
      "(right 2)"
      "(right 3)")
 (pos 14 -7))

;; and since we went through all the argN nonsense, the reverse:
(the-pos (pos 0 0))
(check-equal?
 (run "(move (0 0) arg6 arg5 arg4 arg3 arg2 arg1 arg0)"
      "(right 3)"
      "(right 2)"
      "(up 6)"
      "(down 2)"
      "(right 4)"
      "(right 5)"
      "(up 3)")
 (pos 14 -7))
;; not that it matters with addition and subtraction...why am I even...