-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.lisp
More file actions
executable file
·56 lines (44 loc) · 1.35 KB
/
1.lisp
File metadata and controls
executable file
·56 lines (44 loc) · 1.35 KB
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
#!/usr/bin/env clisp
;;;; Eulerproblem 1
;;;; Find all multiples of 3 or 5 below 1000
;; Command line args
(defun args()
ext:*args*)
(defun arg-n-is-true(n)
(when (<= n (list-length (args)))
(= 0 (parse-integer (nth n (args))))))
;; Should we display test results? arg0
(defvar *test* (arg-n-is-true 0))
;; Should we compute the result? arg1
(defvar *run* (arg-n-is-true 1))
;;; 1) Funtion to determine if arg1 is multiple of 3 or 5
;;; 2) Compute list [0..1000]
;;; 3) Map above list of numbers to a list of numbers that satisfy
;;; above precondition
;;; 4) Sum above list
;; 1)
(defun multipleOf3Or5 (n)
(or (= 0 (mod n 3))
(= 0 (mod n 5))))
;; 2)
(defun list-to-n (n)
(reverse
(let (v)
(dotimes (i n v)
(setq v (cons i v))))))
;; 3) and 4)
(defun result ()
(apply '+ (remove-if-not 'multipleOf3Or5 (list-to-n 1000))))
(defun test (description expected actual)
(format t "~S: ~S ~%" description (equal expected actual)))
(when *test* T
(progn
(test "3 is multiple of 3 or 5" T (multipleOf3Or5 3))
(test "5 is multiple of 3 or 5" T (multipleOf3Or5 5))
(test "9 is multiple of 3 or 5" T (multipleOf3Or5 9))
(test "8 is not multiple of 3 or 5" NIL (multipleOf3Or5 8))
(test "list-to-n can build a list like [0..n]"
'(0 1 2) (list-to-n 3))
))
(when *run* T
(format t "~S" (result)))