0% found this document useful (0 votes)
50 views

Build The List (Foo Bar Baz) From Scratch Using Cons

This document provides examples of Lisp expressions and functions to write and evaluate. It includes expressions to evaluate basic arithmetic operations, functions to calculate things like the average of numbers, factorial of a number, Fibonacci series, and functions to manipulate lists like extracting elements, merging lists, and more. Sample inputs and outputs are provided for each function defined.

Uploaded by

Nilanjan Mondal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Build The List (Foo Bar Baz) From Scratch Using Cons

This document provides examples of Lisp expressions and functions to write and evaluate. It includes expressions to evaluate basic arithmetic operations, functions to calculate things like the average of numbers, factorial of a number, Fibonacci series, and functions to manipulate lists like extracting elements, merging lists, and more. Sample inputs and outputs are provided for each function defined.

Uploaded by

Nilanjan Mondal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write corresponding lisp expressions for the following and evaluate: (+ 12 45) 12+45 (+ 12 45 7 ) !

"r#itrar$ nu%#er of argu%ents 12+45+7 (+ 12 (& 45)) ! 'nar$ %inus 12+(&45) (+ 12 (& 45 7 )) ! (inar$ %inus 12+(45&7 ) (1+ () 2 *)) 1+2)* (1& (+ 2 *)) 1&(2+*) (+ *5 1*) *5+1* (+ *5., 1*.,) *5.,+1*., (+ *5 5) ! -nteger *5+5 (+ *4 5) ! .atio (float (+ *4 5)) ! /loating point nu%#er (& (+ 1 (+ (+ 2 *) () 4 5))) 0) 11(2+*+4)5)&0 (2)4)+(0& ) 25+(&*+4)3+(0+2+5) 2.a) 4ow %an$ ele%ents do each of the following lists have5 Write lisp expressions. (6789 :48 76; ("< ;66.= 4">)? ((6789) (:48 76; ("< ;66.=) 4">)? ((1 2 *) (4 5 0) (7 @) (1, 11 12))? ((698) /6. ">> ("9; (:W6 (/6. A8)))) #). /ind out whether the lists (a (# c) d) and ((a #) c d)are eBual. "ns:&(eBual C(a (# c) d) C(a (# c) d)) c) /ind out the first?second?third and rest fro% the following lists: (1 2 * 4)?((i hate) (peanut #utter) (and Dell$))?($ou (walrus (hurt) the (one $ou) love))?(add * (%ult 4 5))?(gar#age (gar#age) out)?(car ((in the garage) parE))? (deeper and (deeper and (deeper and (deeper we went)))) 4int:&'se predicates first? second?rest etc. d) i) 8xtract the ele%ent /-8 fro% the following lists: (/88 /-8 /68 /'A)? ((/88 /-8) (/68 /'A)) ii) 8xtract the ele%ent /68 fro% the following lists: (/88 /-8 /68 /'A) iii) /ind out the third and fourth ele%ent fro% the list of #) 4int:&'sing F". and F;. *. Get each word fro% the lists (((/'9)) (-9 :48) (='9))? (((>'8 F'(8) (.8; 7<."A-;)) 4int:&'sing F". and F;. 4. (uild the list (foo #ar #aH) fro% scratch using cons. 5. a) "ppend lists (1 2 *) and (4 5 0). 4int:&'se append function #) 7ut the s$%#ols IiJ? Ia%J ?IaJ? IstudentJ ?IofJ ?Idiat%J into a list. 4int:&'se list function 0. Write and evaluate the following using lisp predicates and the return t$pe. a) 7 is less than 11 #) 12 is odd c) :he s$%#ols IEirEJ and IspocEJ are eBual d) ItwelveJ is a nu%#er e) &4 is less than &* f) @ and &@ are eBual g) * less than and eBual to @ h) greater than and eBual to i) FhecE whether the following are lists: i) stitch ii) a stitch in ti%e D) FhecE whether the following are ato%s: i) 1 ii) golf iii) (hole in one) E) 7ut the following in the categor$ of string? nu%#er? float? integer? s$%#ol? odd nu%#er? even nu%#er Idiat%J? . ?0? add? *4? 17? Ihello worldJ

"ns:&

(K 7 11)? (oddp 12)? (eBual CEirE CspocE)? (nu%#erp 12)? (K &4 &*)? (eBual &@ @)? (KL* @)? (listp Cstitch)? (listp C(a stitch in ti%e))? (ato% 1 )? (ato% Cgolf)? (ato% C(hole in one))? (stringp Idiat%J)? (floatp . )? (s$%#olp Cdiat%)
7. Write a function? using car and cdr (or first and rest) which returns the third ele%ent of a list. "ns:& (defun our&third (x) (car (cdr (cdr x)))) (our&third C(a # c d)) . Write a function called (seconds&in&a&leap&$ear) which calculates the nu%#er of seconds in a leap $ear. "ns:&(defun seconds&in&a&leap&$ear () () 0, () 0, () 24 *00)))) @. Write a function (less&than&or&eBual&to x $)which returns t if x is less than or eBual to $. ;efine it using a logical operators and? or and not. "ns:&(defun less&than&or&eBual&to (x $) (or (K x $) (L x $))) 1,. Fo%pute three ti%es of x. "ns:&(defun triple (M) NFo%pute three ti%es M.N ! -nline co%%ents can () * M)) ! #e placed here. 11. 9egate the sign of a nu%#er "ns:&(defun negate (M) N9egate the value of M.N ! :his is a docu%entation string. (& M)) 12. Write a function that will taEe two aregu%ents x?$. :he output will #e the value of x+$+5. "ns:&defun foo (x $) (+ x $ 5)) !defining a function /66 O (foo 5 ,) !calling a function 1, 1*. /ind the average of two nu%#ers. "ns:&(defun average (x $) (+ (+ x $) 2.,)) 14. ;efine a predicate called >69G8.&:4"9 that taEes two lists as input and returns : if the first list is longer than the second. Ans: (defun longer-than (x y) (> (length x) (length y))) 15. . Write a function ";;>89G:4 that taEes a list as input and returns a new list with the length of the input added onto the front of it. -f the input is (A66 G66 G"- 7"9)? the output should #e (4 A66 G66 G"- 7"9). What is the result of (";;>89G:4 (";;>89G:4 P(" ( F)))5 Ans:-(defun addlength (x) (cons (length x) x)) (addlength (addlength (a b c))) (4 3 a b c) 10. Write a predicate /-.=:7 that returns : if its first argu%ent (a s$%#ol) is eBual to the first ele%ent of its second argu%ent (a list). :hat is? (/-.=:7 P/66 P(/66 (". ("Q)) should return :. (/-.=:7 P(6-9G P(/66 (". ("Q)) should return 9->. Ans:- (defun firstp (x y) (equal x (first y)))

17. Write a function A-;&";;1 that adds 1 to the %iddle ele%ent of a three&ele%ent list. /or exa%ple? (A-;&";;1 P(:"R8 2 F66R-8=)) should return the list (:"R8 * F66R-8=). Note: <ou are not allowed to %aEe A-;&";;1 a function of three inputs. -t has to taEe a single input that is a list of three ele%ents. "ns:& (defun %id&add1 (x) (list (first x) (+ 1 (second x)) (third x))) 1 . . Write a function /&:6&F that converts a te%perature fro% /ahrenheit to Felsius. :he for%ula for doing the conversion is: Felsius te%perature L S5(/ahrenheit te%perature & *2)T+@. :o go in the opposite direction? the for%ula is: /ahrenheit te%perature L(@+5 Felsius te%perature) + *2. Ans:- (defun f-to-c (temperature) (* (- temperature 32.0) 5/9)) 1@. Write a function to find the average of two nu%#ers. (defun average (x y) Ans:-(/ (+ x y) 2.0)) 2,. Write definitions for 4">/ and F'(8 using ;8/'9. (:he F'(8 function should taEe a nu%#er n as input and return n*.) Ans:-(defun half (n) (/ n 2)) (defun cube (n) (* n n n)) 21. ;efine a function 7<:4"G that taEes two inputs? x and y? and returns the sBuare root of x2+y2. <ou %a$ recogniHe this as 7$thagorasPs for%ula for co%puting the length of the h$potenuse of a right triangle given the lengths of the other two sides. (7<:4"G * 4) should return 5.,. "ns:& (defun pythag (x y) (sqrt (+ (* x x) (* y y)))) 22. Write a function A"R8&8U89 that %aEes an odd nu%#er even #$ adding one to it. -f the input to A"R8&8U89 is alread$ even? it should #e returned unchanged. "ns:& (defun %aEe&even (n) (if (evenp n) n (+ n 1))) 2*. Write a function /'.:48. that %aEes a positive nu%#er larger #$ adding one to it? and a negative nu%#er s%aller #$ su#tracting one fro% it. What does $our function do if given the nu%#er ,5 "ns:& (defun further (n) (if (< n 0) (- n 1) (+ n 1))) 24. Write a >isp function (fact n) that co%putes the factorial of n? using a loop. 'se a let to introduce an$ local varia#les that $ou need. "ssu%e that the para%eter is a non&negative integer. (:r$ out $our function to co%pute the factorial of 1,,,,.) "ns:&Aethod 1) (defun fact (n) (cond ((Herop n) 1) (t () n (fact (& n 1)))))) Aethod 2) (defun it&fact (n) (let ((prod 1)) (doti%es (i n prod) (setf prod () prod (+ i 1)))))) (it&fact 5) V 12, 1 . Write down a progra% in lisp to print fi#onacci series using recurssion. "ns:& (defun fi# (n) (cond ((eBual n ,) 1)

((eBual n 1) 1) (t (+ (fi# (& n 1)) (fi# (& n 2)))))) 1@. a) Write a recursive function "9<6;;7 that returns : if an$ ele%ent of a list of nu%#ers is odd. -t returns 9-> if none of the% are. "ns:& (defun an$oddp (x) (cond ((null x) nil) ((oddp (first x)) t) (t (an$oddp (rest x))))) #) Write the a#ove progra% using if instead of cond. "ns:& (defun an$oddp (x) (if x (if (oddp (first x)) (first x) (an$oddp (rest x))))) 2,. Write ">>6;;7? a recursive function that returns : if all the nu%#ers in a list are odd. "ns:& (defun all&oddp (x) (cond ((null x) t) ((evenp (first x)) nil) (t (all&oddp (rest x))))) 21. Write a recursive function ";;&9'A= that adds up the nu%#ers 9? 9&1? 9&2? and so on? down to ,? and returns the result. /or exa%ple? (";;&9'A= 5) should co%pute 5+4+*+2+1+,? which is 15. "ns:& (defun add&nu%s (n) (cond ((Herop n) ,) (t (+ n (add&nu%s (& n 1)))))) 22. a)Write F6'9:&;6W9? a function that counts down fro% n using recursion. (F6'9:&;6W9 5) should produce the list (5 4 * 2 1). #) Write =W'".8&>-=:? a recursive function that taEes a list of nu%#ers as input and returns a list of their sBuares. (=W'".8&>-=: P(* 4 5 0)) should return (@ 10 25 *0). "ns:&a) (defun count&down (n) (cond ((Herop n) nil) (t (cons n (count&down (& n 1)))))) #) (defun sBuare&list (x) (cond ((null x) nil) (t (cons () (first x) (first x)) (sBuare&list (rest x)))))) 23. Write a function F69=:."-9 that taEes three inputs called M? A"M? and A-9. -f M is less than A-9? it should return A-9! if M is greater than A"M? it should return A"M. 6therwise? since M is #etween A-9 and A"M? it should return M. (F69=:."-9 * &5, 5,) should return *. (F69=:."-9 @2 &5, 5,) should return 5,. Write one version using F69; and another using nested -/s. "ns:& (defun constrain (x %ax %in) (cond ((K x %in) %in) ((O x %ax) %ax) (t x))) (defun constrain (x %ax %in) (if (K x %in) %in (if (O x %ax) %ax x))) 24. Write a function 8A74"=-Q8 that changes the first word of a phrase fro% IgoodJ to IgreatJ 6. fro% I#adJ to IawfulJ and returns the %odified phrase. "ns:& (defun e%phasiHe (x)

(cond ((eBual (first x) Pgood) (cons Pgreat (rest x))) ((eBual (first x) P#ad) (cons Pawful (rest x))))) 25. a) Write 8U8.<&6:48.? a recursive function that returns ever$ other ele%ent of a listXthe first? third? fifth? and so on. (8U8.<&6:48. P(" ( F ; 8 / G)) should return (" F 8 G). (8U8.<&6:48. P(- F"A8 - ="W - F69W'8.8;)) should return (- - -). #) Write A8.G8&>-=:=? a function that taEes two lists of nu%#ers? each in increasing order? as input. :he function should return a list that is a %erger of the ele%ents in its inputs? in order. (A8.G8&>-=:= P(1 2 0 1, 12) P(2 * 5 @ 1*)) should return (1 2 2 * 5 0 @ 1, 12 1*). "ns:&a) (defun ever$&other (x) (cond ((null x) nil) (t (cons (first x) (ever$&other (rest x)))))) #) (defun %erge&lists (x $) (cond ((null x) $) ((null $) x) ((K (first x) (first $)) (cons (first x) (%erge&lists (rest x) $))) (t (cons (first $) (%erge&lists x (rest $)))))))

1. a) .epresent the a#ove fa%il$ tree using 7.6>6G clauses. "ns: parent( pa%? #o#). parent( to%? #o#). parent( to%? liH). parent( #o#? ann). parent( #o#? pat). parent( pat? Di%). #) 4ow will $ou asE the following Bueries using 7.6>6G5 i) -s (o# parent of 7at5

ii) -s >iH parent of 7at5 iii) -s :o% parent of (en5 iv) Who is >iHPs parent5 v) Who is #o#Ps children5 vi) Who is parent of who%5 vii) Who is a parent of Yi%5 "ssu%e that this is so%e <. Who is a parent of <5 "ssu%e that this is so%e M . viii) ;o "nn and 7at have a co%%on parent5 :his can #e expressed again in two steps: (1) Who is a parent? M? of "nn5 (2) -s (this sa%e) M a parent of 7at5 "ns: i) 5& parent( #o#? pat). ii) 5& parent( liH? pat). iii) 5& parent( to%? #en). iv) 5& parent( M? liH). v) 5& parent( #o#? M). vi) 5& parent(M? <). vii) 5& parent( <? Di%)? parent( M? <). Uiii) 5& parent( M? ann)? parent( M? pat). c) "ssu%ing the parent relation as defined in this section? what will #e 7rologZs answers to the folrowing Buestions5 i) 5&parent( Di%? M). ii) 5&parent( M? Di%). iii) 5&parent( paffi? M)? parent( M? pat). iv) 5&parent( paffi? M)? parent( M? <)? parent( <? Di%). "ns:&

d) /or%ulate in 7rolog the following Buestions a#out the parent relation: i) Who is 7atZs parent5 ii) ;oes >iH have a child5 iii) Who is 7atZs grandparent5 "ns:&

e) -n the progra% of Buestion 1a) introduce the relation %ale and fe%ale either using one unar$ or a #inar$ relation. "ns:&

6.

f) -ntroduce 6ffspring relation using 7arent relation. "ns:& /or all M and <? < is an offspring of M if M is a parent of <. offspring( <? M) :& parent( M? <). g) -ntroduce Aother relation using 7arent anf /e%ale relation. "ns:& /or all M and <. M is the %other of < if M is a parent of < and M is a fe%ale. %other( M? <) :& parent( M? <)? fe%ale( M). 2. :ranslatet he followings tate%entsin to prolog rules: (a) 8ver$#od$ who has a child is happ$ (introduce a one&argu%ent relation happ$). (#) /or all M? if M has a child who has a sister then M has two children (introduce new relation hastwochildren) "ns:& (u) happ$( M) .&parent( M? <). (#) hastwochildren( M) :&parent( M? <)?sister( Q? <). *. -ntroduce 7redecessor relation using recursion. "ns:& /or all M and Q. M is a predecessor of Q if there is a < such that (1) M is a parent of < and (2) < is a predecessor of.Q . predecessor(M?Q) :&parent( M? Q). predecessor(M? Q) :&parent( M? <)?predecessor(<?Q). 4. Write down an entire fa%il$ progra% fro% the fa%il$ tree of the first page. "ns:&

You might also like