[racket] please i need your help using DrRacket
Dear all
how can i edit this code in order to compute the 2 power any number as
argument for example 2 power 5 = 32:
#lang plai
;; (interp (parse '5)(list (fundef 'null 'n (id 'n))))
;; (interp (parse '{+ 5 5})(list (fundef 'null 'n (id 'n))))
;; (interp (parse '{with {x {+ 5 5}} {+ x x}})(list (fundef 'null 'n
(id 'n))))
;; (interp (parse '{with {x 5} {+ x x}})(list (fundef 'null 'n (id 'n))))
;; (interp (parse '{with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}})(list
(fundef 'null 'n (id 'n))))
;; (interp (parse '{with {x 5} {with {y {- x 3}} {+ y y}}}) (list
(fundef 'null 'n (id 'n))))
;; (interp (parse '{with {x 5} {+ x {with {x 3} 10}}})(list (fundef 'null 'n
(id 'n))) )
;; (interp (parse '{with {x 5} {+ x {with {x 3} x}}})(list (fundef 'null 'n
(id 'n))))
;; (interp (parse '{with {x 5} {+ x {with {y 3} x}}})(list (fundef 'null 'n
(id 'n))))
;; (interp (parse '{with {x 5} {with {y x} y}})(list (fundef 'null 'n
(id 'n))))
;; (interp (parse '{with {x 5} {with {x x} x}})(list (fundef 'null 'n
(id 'n))))
(define-type F1WAE
[num (n number?)]
[add (lhs F1WAE?)(rhs F1WAE?)]
[mul (lhs F1WAE?)(rhs F1WAE?)]
[with (name symbol?) (named-expr F1WAE?) (body F1WAE?)]
[id (name symbol?)]
[app (fun-name symbol?) (arg F1WAE?)]
)
(define-type FunDef
[fundef (fun-name symbol?)
(arg-name symbol?)
(body F1WAE?)])
;; newest form of parse, with apply added in.
(define parse
(lambda (sexp)
(cond
[(number? sexp) (num sexp)]
[(symbol? sexp) (id sexp)]
[(list? sexp)
(case (first sexp)
[(+)(add (parse (second sexp))
(parse (third sexp)))]
[(*) (mul (parse (second sexp))
(parse (third sexp)))]
[(with) (with (first (second sexp))
(parse (second (second sexp)))
(parse (third sexp)))]
[else (app (first sexp)
(parse (second sexp)))]
)])))
;; interp: F1WAE listof(fundef) -> number
;; evaluates F1WAE expressions by reducing them to their corresponding
values
(define interp
(lambda (expr fundefs)
(type-case F1WAE expr
[num (n) n]
[add (l r) (+ (interp l fundefs) (interp r fundefs))]
[mul (l r) (* (interp l fundefs) (interp r fundefs))]
[with (bound-id named-expr bound-body)
(interp (subst bound-body
bound-id
(num (interp named-expr fundefs)))
fundefs)]
[id (v) (error 'interp "Free identifier")]
[app (fun-name arg-expr)
(local ([define the-fun-def (lookup-fundef fun-name
fundefs)])
(interp (subst (fundef-body the-fun-def)
(fundef-arg-name the-fun-def)
(num (interp arg-expr fundefs)))
fundefs))])))
;; helper functions
(define lookup-fundef
(lambda (fun-name fundefs)
(cond
[(empty? fundefs)
(error 'lookup-fundef "Function does not exist")]
[else (if (symbol=? fun-name (fundef-fun-name (first fundefs)))
(first fundefs)
(lookup-fundef fun-name (rest fundefs)))])))
(define subst
(lambda (expr sub-id val)
(type-case F1WAE expr
[num (n) expr]
[add (l r) (add (subst l sub-id val)
(subst r sub-id val))]
[mul (l r) (mul(subst l sub-id val)
(subst r sub-id val))]
[with (bound-id named-expr bound-body)
(if (symbol=? bound-id sub-id)
(with bound-id
(subst named-expr sub-id val)
bound-body)
(with bound-id
(subst named-expr sub-id val) ;; added to
take care of scope
(subst bound-body sub-id val)))]
[id (v)(if (symbol=? v sub-id) val expr)]
[app (fun-name arg-expr)
(app fun-name (subst arg-expr sub-id val))])))
;; test
(interp (parse '{double 5})
(list (fundef 'double 'n
(add (id 'n) (id 'n))
)))
--
This message has been scanned for viruses and
dangerous content by SKITCE MailScanner, and is
believed to be clean.