(write-line "Hello World") ;get the list of all atoms ;(((A B) C) (D E)) ==> (A B C D E) (DEFUN fct(L) (COND ((NULL L) NIL) ((ATOM (CAR L)) (CONS (CAR L) (fct (CDR L)))) ((LISTP (CAR L)) (APPEND (fct (CAR L)) (fct (CDR L)))) ) ) (write(fct '(((A B) C) (D E) F) ))
;a) ;sum of two vectors (DEFUN sum(V) (COND ((NULL V) 0) (T (+ (CAR V) (sum (CDR V)))) ) ) (DEFUN add(A B) (+ (sum A) (sum B)) ) (write(add '(1 2 3 4) '(1 1 1 1 1))) (terpri) ;b) ;get the list of all atoms ;(((A B) C) (D E)) ==> (A B C D E) (DEFUN fct(L) (COND ((NULL L) NIL) ((ATOM (CAR L)) (CONS (CAR L) (fct (CDR L)))) ((LISTP (CAR L)) (APPEND (fct (CAR L)) (fct (CDR L)))) ) ) (write(fct '(((A B) C) (D E) F) )) (terpri) ;c) (DEFUN inv(L C) (COND ((NULL L) C) ((ATOM (CAR L)) (SETQ C (CONS (CAR L) C)) (inv (CDR L) C)) ((LISTP (CAR L)) (APPEND C (CONS (inv (CAR L) ()) (inv (CDR L) ())))) ) ) (WRITE(inv '(A B C (D (E F)) G H I) '())) (terpri) ;d) ;max at superficial level (DEFUN maxi(A B) (COND ((< A B) B) (T A) ) ) (DEFUN supmax(L) (COND ((NULL L) (SETQ M -1000)) ((NUMBERP (CAR L)) (SETQ M (maxi (CAR L) (supmax (CDR L))))) (T (supmax (CDR L))) ) ) (write(supmax '(A 2 (6 B) 1 (5))))
(print (merge 'string (reverse " olleH") (string-trim "other" "theotherWorld!") (lambda (a b) NIL)))
( DEFUN eLiniara(Lista) ( COND ( (NULL Lista) T) ( (ATOM (CAR Lista) ) (eLiniara(CDR Lista) )) ( (NUMBERP (CAR Lista) ) (eLiniara(CDR Lista))) ) ) ( write ( eLiniara '( 1 3 5 7 9 ) ) ) ( write ( eLiniara '( 1 (3 5) 7 9 ) ) )
(print (cons 'a '(b c d))) (cond ((> 1 6) (print 5)) (t (print 6)) ) (let* ((x 2) (y (+ 2 x))) (print (+ x y)) ) (let ((x 1)) (let ((y (+ x 1))) (print (+ y x)) ) ) (defun absdiff(x y) (if (> x y) (- x y) (- y x) ) ) (print "Absolute value:") (print (absdiff 5 8)) (print (third '(a b c d))) (print (sort (list 5 0 7 3 9 1 4 13 23) #'<)) (print (mapcar #'- '(8 12) '(4 5))) (print (funcall #'+ 1 5 6)) (print (apply #'+ '(5 5 6))) (defun make-quote (thing &optional arg) (list thing arg) ) (print (make-quote 'all)) (defun fn (x) (cond ((numberp x) #'+) ((listp x) #'append) ) ) (defun combine(&rest args) (apply (fn (car args)) args) ) (print (combine '(a b))) (defun consr (lst elt) (if (null lst) (list elt) (cons (car lst) (consr (cdr lst) elt)) ) ) (print (consr '(a b c e) 'd)) (print (mapcar #'* '(2 3) '(2 5 6))) (defparameter *pi* 3.14) (print *pi*) (print (boundp '*pi*)) (defconstant limit 100) (print limit)
(defun fun () (write x) ) (defun fun1 () (defvar x 20) (fun) ) (defun fun2 () (defvar x 10) (fun) ) ; try reversing the order of the function calls below (fun2) (fun1)
; 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))
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more