-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_by_5_counter.lsp
29 lines (26 loc) · 1.18 KB
/
divide_by_5_counter.lsp
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
;;Divide by 5 counter
;;Sarah Regan-CS3210 (PPL)- Spring 2018
;;=================================================================================================
;;A function that receives a list of numbers, and returns a count of how
;;many are divisible by 5. Uses a helper predicate function that returns
;;true if its numeric argument is evenly divisible by 5.
;; Parameters:
;; x - a number from the list
;; lst - a list
;; Assumptions:
;; 1. list only contains numbers
(defun mod5 (x)
(= (mod x 5) 0))
(defun div (lst)
(cond ((null lst) 0)
((mod5 (car lst))(+ 1 (div (cdr lst))))
(t (div (cdr lst)))))
;; test plan for Divide by 5 counter:
;; category/description data expected result
;;-------------------------------------------------------------------------------------------------
;; empty list () 0
;; single atom not div by 5 (1) 0
;; single atom div by 5 (5) 1
;; list with 1 div by 5 (1 2 3 4 5) 1
;; list with more than 1 div by 5 (5 10 6 7) 2
;; list of all div by 5 (5 10 15 20) 4