;; 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-reader.ss" "lang")((modname pset-07-solution) (read-
case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-
decimal #f #t none #f () #t)))
;; DO NOT PUT ANYTHING PERSONALLY IDENTIFYING BEYOND YOUR CWL IN THIS FILE.
;; YOUR CWLs WILL BE SUFFICIENT TO IDENTIFY YOU AND, IF YOU HAVE ONE, YOUR
;; PARTNER.
;;
(require spd/tags)
(@assignment psets/pset-07); Do not edit or remove this tag
(@problem 1)
(@htdd 1String)
;; 1String is String
;; interp. a 1String represents a single letter
;; CONSTRAINT: any 1String is only one letter long
(@htdd ListOf1String)
;; ListOf1String is one of:
;; - empty
;; - (cons 1String ListOf1String)
;; interp. a sequence of letters
(define L1 empty)
(define L2 (list "a" "B" "c"))
(@htdd Operations)
;; Operations is one of:
;; - empty
;; - (cons "keep" Operations)
;; - (cons "space" Operations)
;; - (cons "remove" Operations)
;; interp. a sequence of operations to apply to a list of 1String
;; "keep" means keep the corresponding letter
;; "space" means replace the corresponding letter with a " "
;; "remove" means remove the corresponding letter
(@htdf decoder)
(@signature ListOf1String Operations -> String)
;; produce string containing letters in l with operations applied
(check-expect (decoder empty empty) "")
(check-expect (decoder empty (list "keep")) "")
(check-expect (decoder empty (list "space")) "")
(check-expect (decoder empty (list "remove")) "")
(check-expect (decoder (list "h" "i") empty) "hi")
(check-expect (decoder (list "h" "o" "w") (list "keep" "keep" "keep"))
"how")
(check-expect (decoder (list "o" "h") (list "space" "space"))
" ")
(check-expect (decoder (list "a" "d" "d") (list "remove" "remove"))
"d")
(check-expect (decoder (list "i" "k" "l" "h" "a" "m" "k" "s" "a" "r" "m")
(list "keep" "remove" "remove" "space" "keep" "keep"
, "space" "keep" "keep" "remove" "keep"))
"i am sam")
(check-expect (decoder (list "e" "s" "r" "a" "m" "d" "i" "f" "a" "m")
(list "remove" "keep" "remove" "keep" "keep" "space"
"keep" "space"))
"sam i am")
#|
CROSS PRODUCT OF TYPE-COMMENTS TABLE
\ | |
\ l-> | empty | (cons 1String ListOf1String)
ops \ | |
____V_____________\__|________________|__________________________________
| |
empty | | (string-append (first l) ;(2)
| | (decoder (rest l)
| | empty))
_____________________| |_____________________________________
| |
(cons "keep" | | (string-append (first l) ;(3)
Operations) | | (decoder (rest l)
| | (rest ops)))
_____________________| "" ;(1) |_____________________________________
| |
(cons "space" | | (string-append " " ;(4)
Operations) | | (decoder (rest l)
| | (rest ops)))
_____________________| |_____________________________________
| |
(cons "remove" | | (decoder (rest l) (rest ops)) ;(5)
Operations) | |
|#
(@template 2-one-of)
(define (decoder l ops)
(cond [(empty? l) ""] ;(1)
[(empty? ops) ;(2)
(string-append (first l)
(decoder (rest l)
empty))]
[(string=? "keep" (first ops)) ;(3)
(string-append (first l)
(decoder (rest l)
(rest ops)))]
[(string=? "space" (first ops)) ;(4)
(string-append " "
(decoder (rest l)
(rest ops)))]
[else ;(5)
(decoder (rest l)
(rest ops))]))
;;
;; Problems 2-7.
metadata
;; about the language level of this file in a form that our tools can easily
process.
#reader(lib "htdp-intermediate-reader.ss" "lang")((modname pset-07-solution) (read-
case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-
decimal #f #t none #f () #t)))
;; DO NOT PUT ANYTHING PERSONALLY IDENTIFYING BEYOND YOUR CWL IN THIS FILE.
;; YOUR CWLs WILL BE SUFFICIENT TO IDENTIFY YOU AND, IF YOU HAVE ONE, YOUR
;; PARTNER.
;;
(require spd/tags)
(@assignment psets/pset-07); Do not edit or remove this tag
(@problem 1)
(@htdd 1String)
;; 1String is String
;; interp. a 1String represents a single letter
;; CONSTRAINT: any 1String is only one letter long
(@htdd ListOf1String)
;; ListOf1String is one of:
;; - empty
;; - (cons 1String ListOf1String)
;; interp. a sequence of letters
(define L1 empty)
(define L2 (list "a" "B" "c"))
(@htdd Operations)
;; Operations is one of:
;; - empty
;; - (cons "keep" Operations)
;; - (cons "space" Operations)
;; - (cons "remove" Operations)
;; interp. a sequence of operations to apply to a list of 1String
;; "keep" means keep the corresponding letter
;; "space" means replace the corresponding letter with a " "
;; "remove" means remove the corresponding letter
(@htdf decoder)
(@signature ListOf1String Operations -> String)
;; produce string containing letters in l with operations applied
(check-expect (decoder empty empty) "")
(check-expect (decoder empty (list "keep")) "")
(check-expect (decoder empty (list "space")) "")
(check-expect (decoder empty (list "remove")) "")
(check-expect (decoder (list "h" "i") empty) "hi")
(check-expect (decoder (list "h" "o" "w") (list "keep" "keep" "keep"))
"how")
(check-expect (decoder (list "o" "h") (list "space" "space"))
" ")
(check-expect (decoder (list "a" "d" "d") (list "remove" "remove"))
"d")
(check-expect (decoder (list "i" "k" "l" "h" "a" "m" "k" "s" "a" "r" "m")
(list "keep" "remove" "remove" "space" "keep" "keep"
, "space" "keep" "keep" "remove" "keep"))
"i am sam")
(check-expect (decoder (list "e" "s" "r" "a" "m" "d" "i" "f" "a" "m")
(list "remove" "keep" "remove" "keep" "keep" "space"
"keep" "space"))
"sam i am")
#|
CROSS PRODUCT OF TYPE-COMMENTS TABLE
\ | |
\ l-> | empty | (cons 1String ListOf1String)
ops \ | |
____V_____________\__|________________|__________________________________
| |
empty | | (string-append (first l) ;(2)
| | (decoder (rest l)
| | empty))
_____________________| |_____________________________________
| |
(cons "keep" | | (string-append (first l) ;(3)
Operations) | | (decoder (rest l)
| | (rest ops)))
_____________________| "" ;(1) |_____________________________________
| |
(cons "space" | | (string-append " " ;(4)
Operations) | | (decoder (rest l)
| | (rest ops)))
_____________________| |_____________________________________
| |
(cons "remove" | | (decoder (rest l) (rest ops)) ;(5)
Operations) | |
|#
(@template 2-one-of)
(define (decoder l ops)
(cond [(empty? l) ""] ;(1)
[(empty? ops) ;(2)
(string-append (first l)
(decoder (rest l)
empty))]
[(string=? "keep" (first ops)) ;(3)
(string-append (first l)
(decoder (rest l)
(rest ops)))]
[(string=? "space" (first ops)) ;(4)
(string-append " "
(decoder (rest l)
(rest ops)))]
[else ;(5)
(decoder (rest l)
(rest ops))]))
;;
;; Problems 2-7.