Scheme Racket阴影变量检查

时间:2015-01-20 10:35:56

标签: scheme racket plai

我需要检查的功能是否变量阴影?函数应该返回#t或#f,基于变量是否为阴影。 我使用DrRacket来实现代码(#lang plai)。到目前为止,我有这个......

#lang plai

(define-type WAE
  [num (n number?)]
  [add (lhs WAE?)(rhs WAE?)]
  [sub (lhs WAE?)(rhs WAE?)]
  [with (name symbol?)(named-expr WAE?)(body WAE?)]
  [id (name symbol?)])

(define (parse sexpr)
  (cond
    [(number? sexpr) (num sexpr)]
    [(symbol? sexpr) (id sexpr)]
    [(list? sexpr)
      (case (first sexpr)
        [(add) (add (parse (second sexpr)) (parse (third sexpr)))]
        [(sub) (sub (parse (second sexpr)) (parse (third sexpr)))]
        [(with) (with (first (second sexpr)) 
                      (parse (second (second sexpr)))
                      (parse(third sexpr)) )]]
     [else (error "unexpected token")]))

(define (symbol<? a b)
  (string<? (symbol->string a) (symbol->string b)))

这是检查变量是否被遮蔽的函数,但它不起作用

(define (shadowed? wae)
  (let 
    ([l(type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    (if (remove-duplicates (flatten l) symbol=?) #t #f)))

1 个答案:

答案 0 :(得分:0)

我不知道#lang plai但我相信您的代码的第二部分可能会出现语法错误。

(define (shadowed? wae)
  (let 
   >>> Possible error >>> ([l(type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    >>>> Error >>> (if (remove-duplicates (flatten l) symbol=?) #t #f)))

试试这个并告诉我们。

(define (shadowed? wae)
  (let 
    ([l (type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    (if (remove-duplicates (flatten l)) symbol=?) #t #f))