Files
read-filefile-exists?read-lines
Multiple complex inputs
- Using argunemnts sequentially
- Using arguments in parallel
- Using cross-products of arguments
Sequentials
;; Using arguments sequentially (~ case 1 in the book)
;; - Approach: follow the template for the first
;; until you hit the base case, and
;; then follow the template for the second.
(define (loa-temp loa)
(...
(cond
[(empty? loa) ...]
[(cons? loa)
(...
(first loa) ...
(loa-temp (rest loa)) ...)])))
; Design a function append-lists that
; accepts two lists, and returns a list
; containing all of the elements of the
; first followed by all of the elements
; of the second.
; append-lists : [List-of Any] [List-of Any] -> [List-of Any]
; create a list containing all of the elements of the first
; followed by all the elements of the second
(check-expect
(append-lists '() (list 1 2))
(list 1 2))
(check-expect
(append-lists (list 1 2) '())
(list 1 2))
(check-expect
(append-lists (list "a") (list 1 2))
(list "a" 1 2))
(define (append-list1 l1 l2)
(cond
[(empty? l1) (cond
[(empty? l2) '()]
[(cons? l2)
(cons
(first l2)
(append-lists l1 (rest l2)))])]
[(cons? l1)
(cons
(first l1)
(append-lists (rest l1) l2))]))
;; Improvement:
(define (append-lists l1 l2)
(cond
[(empty? l1) l2]
[(cons? l1)
(cons
(first l1)
(append-lists (rest l1) l2))]))
Parallel
; Using arguments in parallel (cases 2/3 in the book)
; - Approach: follow a "joint" template that considers
; all possible combinations of the inputs
; A NaturalNumber (Nat) is one of:
; - 0
; - (add1 Nat)
(define NAT-0 0)
(define NAT-1 (add1 NAT-0))
(define NAT-2 (add1 NAT-1))
(define (nat-temp nat)
(...
(cond
[(zero? nat) ...]
[(positive? nat)
(... nat ...
(nat-temp (sub1 nat)) ...)])))
; A [List-of Any] is one of:
; - '()
; - (cons Any [List-of Any])
(define (loa-temp loa)
(...
(cond
[(empty? loa) ...]
[(cons? loa)
(...
(first loa) ...
(loa-temp (rest loa)) ...)])))
The combined template where we go through all combinations is
(define (loa-nat template loa nat)
(...
(cond
[(and (empty? loa) (zero? nat)) (...)]
[(and (empty? loa) (positive? nat)) (... nat ...)]
[(and (cons? loa) (zero? nat)) (... (first loa) ...)]
[(and (cons? loa) (positive? nat))
(... (first loa) ...
nat ...
(loa-nat-temp (rest loa) (sub1 nat) ...))])))
Example:
; Design a function get-element that accepts
; a list and a Nat and returns the element at
; the location in the list specified by the Nat.
; If there is no such element, it should raise
; an error.
; get-element : [List-of Any] Nat -> Any
; Gets the element in the specified location in the list,
; or raises an error if invalid
(check-error (get-element '() 0))
(check-error (get-element '() 1))
(check-expect (get-element (list 1 2 4) 0) 1)
(check-expect (get-element (list 1 2 4) 1) 2)
(check-expect (get-element (list 1 2 4) 2) 4)
(check-error (get-element (list 1 2 3) 3))
(define (get-element loa n)
(cond
[(and (empty? loa) (zero? n)) (error "Invalid index")]
[(and (empty? loa) (positive? n)) (error "Invalid index")]
[(and (cons? loa) (zero? n)) (first loa)]
[(and (cons? loa) (positive? n)) (get-element (rest loa) (sub1 n))]))
Simplification:
(define (get-element loa n)
(cond
[(empty? loa) (error "Invalid index")]
[(and (cons? loa) (zero? n)) (first loa)]
[(and (cons? loa) (positive? n)) (get-element (rest loa) (sub1 n))]))