Question
Please answer the following question in racket language. The template and the ast file is provided below. The answer should strictly follow the template. ast.rkt
Please answer the following question in racket language. The template and the ast file is provided below. The answer should strictly follow the template. "ast.rkt" file
#lang racket (provide (struct-out r:number) (struct-out r:variable) (struct-out r:lambda) (struct-out r:apply) (struct-out r:void) (struct-out r:define) r:expression? r:value? r:term?) ;; Values (define (r:value? v) (or (r:number? v) (r:void? v) (r:lambda? v))) (struct r:void () #:transparent) (struct r:number (value) #:transparent) (struct r:lambda (params body) #:transparent) ;; Expressions (define (r:expression? e) (or (r:value? e) (r:variable? e) (r:apply? e))) (struct r:variable (name) #:transparent) (struct r:apply (func args) #:transparent) ;; Terms (define (r:term? t) (or (r:define? t) (r:expression? t))) (struct r:define (var body) #:transparent) Solution Template: Use this for the solution #lang racket (require "ast.rkt") (require "hw1.rkt") (require rackunit) (provide (all-defined-out))
;; Exercise 5: Parse a quoted AST ;; Solution has 26 lines. ( write your code here) (define (parse-ast node) (define (make-define-func node) 'todo) (define (make-define-basic node) 'todo) (define (make-lambda node) 'todo) (define (make-apply node) 'todo) (define (make-number node) 'todo) (define (make-variable node) 'todo)
(cond [(define-basic? node) (make-define-basic node)] [(define-func? node) (make-define-func node)] [(symbol? node) (make-variable node)] [(real? node) (make-number node)] [(lambda? node) (make-lambda node)] [else (make-apply node)]))
5. (45 points) Recall the AST we defined in Lecture 5. Implement function parse-ast that takes a da- tum and yields an element of the AST. You will need as auxiliary functions real? and symbol? from Racket's standard library and functions lambda?, define-basic?, and define-func? from Homework Assignment 1 (Part II) The function takes a datum that is a valid term. Expression do not include conditional nor booleans, only functions declarations, definitions, variables, and numbers. (check-equal? (parse-ast 'x) (r:variable 'x)) (check-equal? (parse-ast '10) (r:number 10)) (check-equal? (parse-ast (lambda (x) x)) (r:lambda (list (r:variable 'x)) (list (r variable 'x)))) (check-equal? (parse-ast (define (f y) (+ y 10))) (r:define (r:variable 'f) (r:lambda (list (r: variable 'y)) 5. (45 points) Recall the AST we defined in Lecture 5. Implement function parse-ast that takes a da- tum and yields an element of the AST. You will need as auxiliary functions real? and symbol? from Racket's standard library and functions lambda?, define-basic?, and define-func? from Homework Assignment 1 (Part II) The function takes a datum that is a valid term. Expression do not include conditional nor booleans, only functions declarations, definitions, variables, and numbers. (check-equal? (parse-ast 'x) (r:variable 'x)) (check-equal? (parse-ast '10) (r:number 10)) (check-equal? (parse-ast (lambda (x) x)) (r:lambda (list (r:variable 'x)) (list (r variable 'x)))) (check-equal? (parse-ast (define (f y) (+ y 10))) (r:define (r:variable 'f) (r:lambda (list (r: variable 'y))Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started