0

変数がシャドウされているかどうかを確認する関数が必要ですか? 関数は、変数がシャドウ化されているかどうかに基づいて #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)))
4

1 に答える 1

0

わかりません#lang plaiが、コードの 2 番目の部分に構文エラーがあると思います。

(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))
于 2015-01-21T16:07:40.563 に答える