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.
Tutorialspoint

homework

; Assignment Number: 5
; Author: Jacob Daher
; Comment: In this code, we are deciphering Lisp functions that are given to us and how they perform; we then write Java/C++ equivalents to the given function and then afterwards, we write our own functinos to perform various taks, like returning the fibonacci sequence of a number, returning the factorial of a number, etc.


;(defun foo (x y)
   ; (- x y )
;)

;running foo
;(print (foo 4 3))
;running foo
;(print (foo 20 10))
;running foo 
;(print (foo 8 5))

;We are defining a function ID "foo"; this function takes two numbers as parameters and subtracts the second number from the first number, then returns the result of their difference.
;We are printing the return value of the foo function when it is being called. A Java equivalent would be:

;public int foo(int x, int y) {int z = x - y; return z;}

;(defun myPrint (l)
    ;(if (not (null l))
       ; (progn
           ; (print (car l))
            ;(myPrint (cdr l))
       ; )
   ; )
;)

;running myPrint
;(myPrint (quote (a b c d e)))
;;running myPrint
;(myPrint (quote (1 2 3 4 5)))
;running myPrint
;(myPrint (quote ("x" "y" "z")))

;We are defining a function ID "myPrint"; it takes a list as a parameter then uses recursion to traverse through each element in the list. Everytime it calls recursion, it first prints the first symbol in the list, then uses cdr to create a new list that omits said first symbol. This process repeats until there is no more symbols left to omit. A Java equivalent would be:

;public void myPrint(String[] array) {
    
    ;for (int i = 0; i < array.length; i++) { System.out.println(array[i); }
;}


(defun factorial1 (x) 

    (let ((s 1))
        (dotimes (n x) 
            (setf s (* s (+ n 1))) ;Uses dotimes loops to find the factorial of n, by multiplying it by all numbers from 1 to n.
        )
        
        s
    )
)

(defun factorial2 (x)

    (if (<= x 0)  
         1 ;Checks if we've reached the end of the factorial (0), and if so, we return one.
        (* (factorial2(- x 1)) x) ;Performs recursion if we have not reached 0.
    )
)           

(defun number1 (l) 
    
    (let ((x 0))  
    
        (loop for a in l ;repeats the following functions inside the loop function, equal to the number of times in the list.
        
            if (eq a 'z) ;checks if any symbols in the list match the symbol z.
            
                do(
                    setf x (+ x 1) ;Everytime the symbol z is found, one is added to the value of x.
                )
        )
        x
    )
)




(defun fib (n)  
        
        (if (< n 2) 
             n ;If we've reached the last 2 numbers (0 and 1) of our fib sequence, we make n equal to 0/1.
             (+ (fib(- n 1)) (fib(- n 2))) ;Performs recursion until we've reached the last 2 numbers of our sequence.
        )
)

(defun small (x y) 
    
    (if (< x y) x y) ;If x is smaller than y, x will be returned; if y is smaller, then y will be returned.
)

(defun sum (x)
    (let ((summation 0))
        (dotimes (n x) 
        
            (setf summation (+ summation n 1)) ;Adds numbers from 0 to n; the loop calls this function n times.
        )
        summation ;returns our summation value when using the sum function.
    )
)


;(setq x '(a b)) ;setq creates a list with the name x; using quote, we are adding symbols to the list as data as opposed adding them as code, preventing us from evaluating them.
;(setq y '(a b c)) ;setq creates a list with the name y; using quote, we are adding symbols to the list as data as opposed adding them as code, preventing us from evaluating them.
;(print (car x)) ;car returns the first symbol in x's list, that being a. (a symbol is being returned)
;(print (car y)) ;car returns the first symbol in y's list, that being b. (a symbol is being returned)
;(print (cdr x)) ;cdr returns the list containing all of the symbols after the first symbol in x's list, that being b. (a list is being returned)
;(print (cdr y)) ;cdr returns the list containing all of the symbols after the first symbol in y's list, that being b and c. (a list is being returned)
;(print (car (cdr x))) ;first uses cdr to return a new list containg all of the symbols after the first symbol in x's list, then uses car to return the first symbol in that new list (b) (a symbol is being returned)
;(print (car (cdr y))) ;first uses cdr to return a new list containg all of the symbols after the first symbol in y's list, then uses car to return the first symbol in that new list (b) (a symbol is being returned)
;(print (cadr x)) ;cadr returns the symbol in x's list that comes right after the first symbol in x's list, that being b. (a symbol is being return)
;(print (cadr y)) ;cadr returns the symbol in y's list that comes right after the first symbol in y's list, that being b. (a symbol is being return)
;(print (append x y)) ;uses append to merge both lists together and returns a list that contains all the symbols from x and y (a list is being returned)

;test defintion
(defun test() 
    (print "CSE240 Assignment 5. Jacob Daher")
    (print (factorial1 15))
    (print (factorial2 20))
    (print (number1 '(z c (z) a)))
    (print (fib 6))
    (print (small 4 7))
    (print (sum 5))
)

(test)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.