我试图在racket中将sqrt函数添加到我的proffesor语言中,这是语言:
;; ** The MUWAE interpreter
#lang pl 03
#| BNF for the MUWAE language:
<MUWAE> ::= <num>
| { + <MUWAE> <MUWAE> }
| { - <MUWAE> <MUWAE> }
| { * <MUWAE> <MUWAE> }
| { / <MUWAE> <MUWAE> }
| { sqrt <MUWAE> }
| { with { <id> <WAE> } <MUWAE> }
| <id>
|#
;; MUWAE abstract syntax trees
(define-type MUWAE
[Num Number]
[Add MUWAE MUWAE]
[Sub MUWAE MUWAE]
[Mul MUWAE MUWAE]
[Div MUWAE MUWAE]
[sqrt MUWAEE]
[Id Symbol]
[With Symbol MUWAE MUWAE])
(: parse-sexpr : Sexpr -> MUWAE)
;; to convert s-expressions into MUWAEs
(define (parse-sexpr sexpr)
(match sexpr
[(number: n) (Num n)]
[(symbol: name) (Id name)]
[(cons 'with more)
(match sexpr
[(list 'with (list (symbol: name) named) body)
(With name (parse-sexpr named) (parse-sexpr body))]
[else (error 'parse-sexpr "bad `with' syntax in ~s" sexpr)])]
[(list '+ lhs rhs) (Add (parse-sexpr lhs) (parse-sexpr rhs))]
[(list '- lhs rhs) (Sub (parse-sexpr lhs) (parse-sexpr rhs))]
[(list '* lhs rhs) (Mul (parse-sexpr lhs) (parse-sexpr rhs))]
[(list '/ lhs rhs) (Div (parse-sexpr lhs) (parse-sexpr rhs))]
[(list 'sqrt s) (sqrt (parse-sexpr s))]
[else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))
(: parse : String -> MUWAE)
;; parses a string containing a MUWAE expression to a MUWAE AST
(define (parse str)
(parse-sexpr (string->sexpr str)))
#| Formal specs for `subst':
(`N' is a <num>, `E1', `E2' are <MUWAE>s, `x' is some <id>,
`y' is a *different* <id>)
N[v/x] = N
{+ E1 E2}[v/x] = {+ E1[v/x] E2[v/x]}
{- E1 E2}[v/x] = {- E1[v/x] E2[v/x]}
{* E1 E2}[v/x] = {* E1[v/x] E2[v/x]}
{/ E1 E2}[v/x] = {/ E1[v/x] E2[v/x]}
y[v/x] = y
x[v/x] = v
{with {y E1} E2}[v/x] = {with {y E1[v/x]} E2[v/x]}
{with {x E1} E2}[v/x] = {with {x E1[v/x]} E2}
|#
(: subst : MUWAE Symbol MUWAE -> MUWAE)
;; substitutes the second argument with the third argument in the
;; first argument, as per the rules of substitution; the resulting
;; expression contains no free instances of the second argument
(define (subst expr from to)
(cases expr
[(Num n) expr]
[(Add l r) (Add (subst l from to) (subst r from to))]
[(Sub l r) (Sub (subst l from to) (subst r from to))]
[(Mul l r) (Mul (subst l from to) (subst r from to))]
[(Div l r) (Div (subst l from to) (subst r from to))]
[(sqrt s) (sqrt (subst l from to))]
[(Id name) (if (eq? name from) to expr)]
[(With bound-id named-expr bound-body)
(With bound-id
(subst named-expr from to)
(if (eq? bound-id from)
bound-body
(subst bound-body from to)))]))
#| Formal specs for `eval':
eval(N) = N
eval({+ E1 E2}) = eval(E1) + eval(E2)
eval({- E1 E2}) = eval(E1) - eval(E2)
eval({* E1 E2}) = eval(E1) * eval(E2)
eval({/ E1 E2}) = eval(E1) / eval(E2)
eval(id) = error!
eval({with {x E1} E2}) = eval(E2[eval(E1)/x])
|#
(: eval : MUWAE -> Number)
;; evaluates MUWAE expressions by reducing them to numbers
(define (eval expr)
(cases expr
[(Num n) n]
[(Add l r) (+ (eval l) (eval r))]
[(Sub l r) (- (eval l) (eval r))]
[(Mul l r) (* (eval l) (eval r))]
[(Div l r) (/ (eval l) (eval r))]
[(sqrt s) (sqet (eval s))]
[(With bound-id named-expr bound-body)
(eval (subst bound-body
bound-id
(Num (eval named-expr))))]
[(Id name) (error 'eval "free identifier: ~s" name)]))
(: run : String -> Number)
;; evaluate a MUWAE program contained in a string
(define (run str)
(eval (parse str)))
但是当我尝试运行我的代码时,我得到了:
define-type:变体名称已绑定在:sqrt
我在我的bnf stracture中添加了sqrt函数,并添加了所有的eval,subst和sexper,但我猜测我的问题就是结构。
我猜sqrt是一个保存单词,需要覆盖这个单词,这是这样做的方法吗?还有另一种方式吗?
错误意味着什么?
答案 0 :(得分:2)
不试用你的程序:
问题是语言#lang pl 03
已经有sqrt
运算符。
因此该名称已被使用;当你试图给出另一个含义时,你会收到错误“变体名称已经绑定在:sqrt”。
我认为最简单的解决方法是调用Sqrt
的平方根运算符。