Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
; Assignment 5
; Sophia Reed
; Working in Lisp
; #2
; The program subtracts the values passed to varibales x and y and prints the result. Output: 1 10 3
;-----------C++ equivilent-------------------
;#include <iostream>
; using namespace std;
;int foo(int x, int y)
;{
; return (x - y);
;}
; //main method
;int main(){
; cout<<foo(4,3)<< endl;
; cout<<foo(20,10)<< endl;
; cout<<foo(8,5)<< endl;
;}
; #3
; The program checks string and the prints values using recurrsive function
; output: a b c d e 1 2 3 4 5 x y z
;-----------C++ equivilent-------------------
;#include <iostream>
;using namespace std;
;#include <string>
;void myPrint(string l)
;{
; if (!l.empty())
; {
; cout<<l[0]<<endl;
; myPrint(l.erase (0,1));
; }
; }
;//main method
;int main(){
; myPrint(" 1 2 3 4 5");
; myPrint("a b c d e");
; myPrint("x""y""z");
;}
;-------#4------------
(defun factorial1 (n)
(let ((sum 1))
(dotimes (i n)
(setf sum (* sum (1+ i)))
)
sum
)
)
;--------#5----------------
(defun factorial2 (n)
(if(= n 1)
1
(* n (factorial2(- n 1)))))
;------------#6--------------
;
;----------#7------------------
(defun fib (n)
(if (< n 2) n
(+ (fib (1- n)) (fib (- n 2)))
)
)
;------------#8---------------
(defun small(x y)
(if (< x y )
x
y))
;------------#9---------------
(defun sum(n)
(if(= n 0)
0
(+ n (sum (- n 1)))))
;------------#10---------------
;1. setq sets the value of the variable x to the list (a b)
;2. setq sets the value of the variable y to the list (a b c)
;3. prints the first element in list x = A
;4. prints the first element in list y = A
;5. prints the remainding elements in list x with parenthesis. Results in list = (B)
;6. prints the remainding elements in list y with parenthesis. Results in list = (B C)
;7. prints the first element from the rest of the list x without parenthesis. Results in a symbol = B
;8. prints the first element from the rest of the list y without parenthesis. Results in a symbol = B
;9. prints the first element from the rest of the list x without parenthesis. Results in a symbol = B
;10.prints the first element from the rest of the list y without parenthesis. Results in a symbol = B
;11. prints both x and y lists together in one list
;
(setq x '(a b ))
(setq y '(a b c))
(print (car x))
(print (car y))
(print (cdr x))
(print (cdr y))
(print (car(cdr x)))
(print (car (cdr y)))
(print (cadr x))
(print (cadr y))
(print (append x y))
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more