This repository has been archived by the owner on Dec 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment1.txt
111 lines (97 loc) · 2.83 KB
/
assignment1.txt
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;;Steven Glasford
;;Assignment 1
;;CSC372 Brian Slator
;;Problem 1
(print "Problem 1")
(print (mapcar (lambda (x) (* x 2)) '(2 4 6 8)))
(print (mapcar (lambda (x)
(if (numberp x)
(* x 2)
nil))
'(2 4 N 8)))
;;Problem 2
(print "Problem 2")
(defun MAKE-SET (lst)
(cond ((null lst) nil)
((member (car lst) (cdr lst))
(MAKE-SET (cdr lst)))
(t (cons (car lst) (MAKE-SET (cdr lst))))))
(print (MAKE-SET '(A B C A D C)))
;;problem 3
(print "Problem 3")
;;returns a TRUE is the lit is a set, false otherwise
(defun IS-SET (lst)
(if (eq (list-length lst) (list-length (MAKE-SET lst)))
(print "TRUE")
(print "FALSE"))
)
(print "(IS-SET '(a b c d))")
(IS-SET '(a b c d))
(print "(IS-SET '(a a b c d))")
(IS-SET '(a a b c d))
;;Problem 4
;;Write a recursive my-len function that returns the length
;;of a list
(print "Problem 4")
(defun my-len (lst)
(if (null lst)
0
(1+ (my-len (cdr lst)))
)
)
(print "(my-len '(1 2 3 4 5))")
(print (my-len '(1 2 3 4 5)))
;;Problem 5
;;Write a recursive my-sum function that returns
;;the sum of a list
(print "Problem 5")
(defun my-sum (lst)
(if (null lst)
0
(+ (car lst) (my-sum (cdr lst)))
)
)
(print "(my-sum '(1 2 3 4 5 6 7 8 9))")
(print (my-sum '(1 2 3 4 5 6 7 8 9)))
;;Problem 6
;;Use your my-len and my-sum functions to implement a
;;my-average that takes a list of numbers and returns the
;;mean (average) value
(print "Problem 6")
(defun my-average (lst)
(/ (my-sum lst) (my-len lst))
)
(print "(my-average '(1 2 3 4 5 6 7 8 9))")
(print (my-average '(1 2 3 4 5 6 7 8 9)))
(print "(my-average '(123 233 324 445 526))")
(print (my-average '(123 233 324 445 526)))
;;problem 7
;;Write a function that uses my-len function from #7
;;to recursively find and return a sub-list with
;;the maximum length
(print "Problem 7")
(defun max_sub_list (lst)
(cond ((null lst) nil)
((not (listp (car lst))) (max_sub_list (cdr lst)))
((> (my-len (car lst)) (my-len (max_sub_list (cdr lst))))
(car lst))
(t (max_sub_list (cdr lst)))
)
)
(print "(my-average '(123 233 324 445 526))")
(print (max_sub_list '(1 (1 2 3 4) (1 2 3))))
;;problem 8
;;Write a recursive function named MYMEMBER that takes an
;;ATOM (atm) and a LIST (lst) and returns the ATOM if it is a
;;MEMBER, otherwise NIL
(print "Problem 8")
(defun MYMEMBER (atm lst)
(cond ((null lst) nil)
((equal atm (car lst)) atm)
(t (MYMEMBER atm (cdr lst)))
)
)
(print "(MYMEMBER 'D '(A B C D E F G))")
(print (MYMEMBER 'D '(A B C D E F G)))
(print "(my-average '(123 233 324 445 526))")
(print (MYMEMBER 'H '(A B C D E F G)))