-
Notifications
You must be signed in to change notification settings - Fork 0
/
139.rkt
executable file
·55 lines (45 loc) · 1.8 KB
/
139.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
;; 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-beginner-reader.ss" "lang")((modname |139|) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp")) #f)))
; A List-of-amounts is one of:
; - '()
; (cons PositiveNumber List-of-amounts)
; example: (cons 1 '())
; List-of-amounts -> Number
; computes the sum of amounts
(check-expect (sum '()) 0)
(check-expect (sum (cons 1 '())) 1)
(check-expect (sum (cons 2 (cons 1 '()))) 3)
(define (sum loa)
(cond
[(empty? loa) 0]
[else (+ (first loa) (sum (rest loa)))]))
; A List-of-numbers is one of:
; – '()
; – (cons Number List-of-numbers)
; List-of-numbers -> Boolean
; are all numbers in List-of-numbers are positive numbers
(check-expect (pos '()) #true)
(check-expect (pos (cons 5 '())) #true)
(check-expect (pos (cons 10 (cons 5 '()))) #true)
(check-expect (pos (cons -1 '())) #false)
(check-expect (pos (cons -1 (cons 5 '()))) #false)
(check-expect (pos (cons 1 (cons -5 '()))) #false)
(define (pos lon)
(cond
[(empty? lon) #true]
[else (and (> (first lon) 0) (pos (rest lon)))]))
(pos (cons 10 (cons 5 '())))
(pos (cons -1 (cons 5 '())))
(define ERROR "provided lon is not List-of-amounts")
; List-of-numbers -> Number
; produces sum of List-of-numbers if it is List-of-amounts
; otherwise signals an error
(check-error (checked-sum (cons 3 (cons -1 (cons 2 '())))) ERROR)
(check-expect (checked-sum '()) 0)
(check-expect (checked-sum (cons 1 '())) 1)
(check-expect (checked-sum (cons 2 (cons 1 '()))) 3)
(define (checked-sum lon)
(cond
[(pos lon) (sum lon)]
[else (error ERROR)]))