-
Notifications
You must be signed in to change notification settings - Fork 0
/
353.rkt
64 lines (54 loc) · 2.52 KB
/
353.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname 353ex) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
#|
+-----------------------------------------------------------------------------+
| Exercise 353. |
| ------------- |
| Design the numeric? function. It determines whether a BSL-var-expr is also |
| a BSL-expr. Here we assume that your solution to exercise 345 is the |
| definition for BSL-var-expr without Symbols. |
+-----------------------------------------------------------------------------+
|#
;; BSL-var-exp -> Boolean
;; is ex a BSL-exp?
#;
(define (numeric? ex)
#false)
(define-struct add [left right])
; An Add is a Structure:
; (make-add BSL-var-expr BSL-var-expr)
; interpretation. (make-add BSL-var-expr BSL-var-expr) shows
; the addition of two BSL-expr
(define-struct mul [left right])
; A Mul is a structure:
; (make-mul BSL-var-expr BSL-var-expr)
; interpretation (make-mul BSL-var-expr BSL-var-expr) shows
; the multiplication of two BSL-expr
; A BSL-var-expr is one of:
; – Number
; – Symbol
; – (make-add BSL-var-expr BSL-var-expr)
; – (make-mul BSL-var-expr BSL-var-expr)
;; BSL-var-expr -> BSL-var-expr
;; replaces all occurrences of x with v
#;
(define (bsl-expr-temp bsl-expr)
(cond
[(number? bsl-expr) ...]
[(symbol? bsl-expr) ...]
[(add? bsl-expr) (... (bsl-expr-temp (add-left bsl-expr)) ... (bsl-expr-temp (add-right bsl-expr))...)]
[(mul? bsl-expr) (... (bsl-expr-temp (mul-left bsl-expr)) ... (bsl-expr-temp (mul-right bsl-expr))...)]))
(check-expect (numeric? 1) #true)
(check-expect (numeric? 'v) #false)
(check-expect (numeric? (make-add 1 23)) #true)
(check-expect (numeric? (make-mul 1 23)) #true)
(check-expect (numeric? (make-mul 1 'c)) #false)
(check-expect (numeric? (make-add (make-mul 1 23) (make-mul 1 23))) #true)
(check-expect (numeric? (make-add (make-mul 'c 23) (make-mul 1 23))) #false)
(define (numeric? bsl-expr)
(cond
[(number? bsl-expr) #true]
[(symbol? bsl-expr) #false]
[(add? bsl-expr) (and (numeric? (add-left bsl-expr)) (numeric? (add-right bsl-expr)))]
[(mul? bsl-expr) (and (numeric? (mul-left bsl-expr)) (numeric? (mul-right bsl-expr)))]))