-
Notifications
You must be signed in to change notification settings - Fork 0
/
350.rkt
61 lines (50 loc) · 1.96 KB
/
350.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
;; 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 350ex) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
#|
+-----------------------------------------------------------------------------+
| Exercise 350. |
| ------------- |
| What is unusual about the definition of this program with respect to the |
| design recipe? |
+-----------------------------------------------------------------------------+
|#
; An S-expr is one of:
; – Atom
; – SL
; An SL is one of:
; – '()
; – (cons S-expr SL)
; An Atom is one of:
; – Number
; – String
; – Symbol
;; -----------------------------------------------------------------------------
; S-expr -> BSL-expr
(define (parse s)
(cond
[(atom? s) (parse-atom s)]
[else (parse-sl s)]))
; SL -> BSL-expr
(define (parse-sl s)
(local ((define L (length s)))
(cond
[(< L 3) (error WRONG)]
[(and (= L 3) (symbol? (first s)))
(cond
[(symbol=? (first s) '+)
(make-add (parse (second s)) (parse (third s)))]
[(symbol=? (first s) '*)
(make-mul (parse (second s)) (parse (third s)))]
[else (error WRONG)])]
[else (error WRONG)])))
; Atom -> BSL-expr
(define (parse-atom s)
(cond
[(number? s) s]
[(string? s) (error WRONG)]
[(symbol? s) (error WRONG)]))
;; -----------------------------------------------------------------------------
; the template for SL calls for empty? and cons? predicate, this is not
; followed in parse-sl (which decides based on <, =, and > the length of the
; expression)