;Assignment 1 - LISP assignment ;Nikolas Kukert ;CSCI 372 - Brian Slator, Ph.D. ;10/12/2018 (defun MY-DOUBLE-ODD (lis) (cond ((null lis) nil) ;terminal base case of empty list ((eq (rem (car lis) 2) 0) ;check for evenness (cons (car lis) (my-double-odd(cdr lis))));move on past even number (t (cons (* 2 (car lis)) (my-double-odd(cdr lis)))));double odd number and move on to next ) ;end function ;a function to double odd numbers and leave even numbers unchanged (defun doubleodd (x) (cond ((eq (rem x 2) 0) ;check for evenness (setq x x)) ;move on past even number (t (setq x (* x 2) )) ;double odd number and move on to next ) ;end of conditional statement ) ;a function to double all of the odd numbers in a list (defun MY-NR-DOUBLE-ODD (lis) (mapcar #'doubleodd lis) ;iteratively call the doubleodd function on each of the elements in the list ) ;end function (defun COUNTNUM (lis) (cond ((null lis) 0) ;return 0 if list is empty (t (+ 1 (COUNTNUM (cdr lis))))) ;add one and recursively call countnum on the remainder of the list ) (defun NUMSUM (lis) (cond ((null lis) 0) ;return 0 if list is empty (t (setq x (car lis)) ;set atom to car of list (+ x (numsum (cdr lis)))) ;add recursive call to numsum to atom x, repeating until list is empty ) );end function (defun MYAVERAGE (lis) (cond ((null lis) "Empty list") (t(/ (numsum lis) (countnum lis)))) ;divide sum of values by count of values ) (defun MY-LENGTH (lis) (setq x 0) ;set counter to 0 (dolist (i lis) ;iterate through the list (setq x (+ x 1)) ;count once for each iteration ) (setq ret x) ;return count ) (defun WHICH-LONG (lis1 lis2) (setq x 0) ;sets value for return (setq first (my-length lis1)) ;get length of first list (setq second (my-length lis2)) ;get length of second list (cond ((= first second) (setq x(+ first second))) ;return sum of lengths if equal ((> first second) (setq x first)) ;return length of first if first is longer (t(setq x second))) ;return length of second if second is longer ) (format t "my-double-odd '(1 2 3 4 5 6) returns ~S~%" (my-double-odd '(1 2 3 4 5 6))) (format t "boundary case of '(0 0 0 0 0) returns ~S~%" (my-double-odd '(0 0 0 0 0))) (format t "boundary case of '(-1 -2 -3 -4 -5 -6) returns ~S~%" (my-double-odd '(-1 -2 -3 -4 -5 -6))) (format t "my-nr-double-odd '(1 2 3 4 5 6) returns ~S~%" (my-nr-double-odd '(1 2 3 4 5 6))) (format t "boundary case of '(0 0 0 0 0) returns ~S~%" (my-nr-double-odd '(0 0 0 0 0))) (format t "boundary case of '(-1 -2 -3 -4 -5 -6) returns ~S~%" (my-nr-double-odd '(-1 -2 -3 -4 -5 -6))) (format t "countnum '(1 2 3 4 5) returns ~S~%" (countnum '(1 2 3 4 5))) (format t "boundary case of '() returns ~S~%" (countnum '())) (format t "numsum '(1 2 3 4 5) returns ~S~%" (numsum '(1 2 3 4 5))) (format t "boundary case of '() returns ~S~%" (numsum '())) (format t "boundary case of '(-1 -2 -3 -4 -5) returns ~S~%" (numsum '(-1 -2 -3 -4 -5))) (format t "myaverage '(1 2 3 4 5) returns ~S~%" (myaverage '(1 2 3 4 5))) (format t "boundary case of '() returns ~S~%" (myaverage '())) (format t "my-length '(1 0 -50 400 9) returns ~S~%" (my-length '(1 0 -50 400 9))) (format t "boundary case of '() returns ~S~%" (my-length'())) (format t "which long '(1 2 3) '(7 8 9 10) returns ~S~%" (which-long '(1 2 3) '(7 8 9 10))) (format t "boundary case of equal lists returns the sum of the lengths, ~S~%" (which-long '(1 2 3) '(4 5 6)))
(make-package :tom) (make-package :dick) (make-package :harry) (in-package tom) (defun szia() (write-line "sziaaaa")) (in-package dick) (defun szia() (write-line "see yaa")) (in-package harry) (defun szia () (Write-line "si ia")) (in-package tom) (szia) (in-package dick) (szia) (in-package harry) (szia) (delete-package tom);itt a törlés (in-package tom) (szia)
;Copyright (c) 2014 Godly T.Alias ;returns the quantity in first jug (defun get-first-jug (state) (car state)) ;returns the quantity in second jug (defun get-second-jug (state) (cadr state)) ;returns the state of two jugs (defun get-state (f s) (list f s)) ;checks whether a given state is a goal ; GOAL IS TO GET 4 IN SECOND JUG (defun is-goal (state) (eq (get-second-jug state) 4)) ;returns all possible states that can be derived ;from a given state (defun child-states (state) (remove-null (list (fill-first-jug state) (fill-second-jug state) (pour-first-second state) (pour-second-first state) (empty-first-jug state) (empty-second-jug state)))) ;remove the null states (defun remove-null (x) (cond ((null x) nil) ((null (car x)) (remove-null (cdr x))) ((cons (car x) (remove-null (cdr x)))))) ;return the state when the first jug is filled (first jug can hold 3) (defun fill-first-jug (state) (cond ((< (get-first-jug state) 3) (get-state 3 (get-second-jug state))))) ;returns the state when the second jug is filled (second jug can hold 5) (defun fill-second-jug (state) (cond ((< (get-second-jug state) 5) (get-state (get-first-jug state) 5)))) ;returns the state when quantity in first ;is poured to second jug (defun pour-first-second (state) (let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond ((zerop f) nil) ; first jug is empty ((= s 5) nil) ; Second jug is full ((<= (+ f s) 5) (get-state 0 (+ f s))) (t ; pour to first from second (get-state (- (+ f s) 5) 5))))) ;returns the state when second jug is poured to first (defun pour-second-first (state) (let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond ((zerop s) nil) ; second jug is empty ((= f 3) nil) ; second jug is full ((<= (+ f s) 3) (get-state (+ f s) 0)) (t ;pour to second from first (get-state 3 (- (+ f s) 3)))))) ;returns the state when first jug is emptied (defun empty-first-jug (state) (cond ((> (get-first-jug state) 0) (get-state 0 (get-second-jug state))))) ;returns the state when second jug is emptied (defun empty-second-jug (state) (cond ((> (get-second-jug state) 0) (get-state (get-first-jug state) 0)))) ;;;MAIN FUNCTION (defun dfs (start-state depth lmt) (setf *node* 0) (setf *limit* lmt) (dfs-node start-state depth) ) ;dfs-node expands a node and calls dfs-children to recurse on it (defun dfs-node (state depth) (setf *node* (+ 1 *node*)) (cond ((is-goal state) (list state)) ((zerop depth) nil) ((> *node* *limit*) nil) ((let ((goal-child (dfs-children (child-states state) (- depth 1)))) (and goal-child (cons state goal-child)))) ;for back-tracking if the branch don't have a goal state )) ;dfs-children expands each node recursively and give it ;to dfs-node to process (defun dfs-children (states depth) (cond ((null states) nil) ((dfs-node (car states) depth)) ((dfs-children (cdr states) depth)))) (print "ENTER YOUR INPUT AS") (print "(dfs start_state depth limit)")
; TASK 1 (PRINT ((LAMBDA (X Y Z) ( LIST (CAR X) (CAR Y) (CAR Z))) `(PRIM SD FLAG() (GHG)) `(1 56 98 52) `(T 2 3 4 Y H))) ; TASK 2 (DEFUN FUNC1 (X Y Z) (LIST (NTH 4 X) (NTH 2 Y) (NTH 1 Z))) (PRINT(FUNC1 `(PRIM SD FLAG() (GHG)) `(1 56 98 52) `(T 2 3 4 Y H))) ;TASK 3 (DEFUN FUNC2 (X) (COND ((AND (SYMBOLP (NTH 0 X)) (SYMBOLP (CAR (LAST X)))) (LIST (NTH 0 X) (CAR(LAST X)))) ( T (CONS (CAR X) (CDDR X))))) (PRINT(FUNC2 '( D 1 2 3 4 E))) (PRINT(FUNC2 '( (D) 1 2 3 4 E)))
;SUM = X * (1 + p*d/y)n ;X - начальная сумма; ;% - процентная ставка, процентов годовых /100; ;n - количество периодов, лет (месяцев, кварталов). ; p - процентная ставка (процентов годовых / 100) по вкладу, ;например, если ставка 10,5%, то p = 10,5 / 100 = 0,105; ;d - период (количество дней), по итогам которого происходит капитализация (начисляются проценты), ;например, если капитализация ежемесячная, то d = 30 дней ;если капитализация раз в 3 месяца, то d = 90 дней; ;y - количество дней в календарном году (365 или 366). (defun bank_proc (p d y)(* (procent p) (/ d y))) (defun procent (p)(/ p 100.0)) (defun complex_proc (summa stavka nperiodov kapital dneyvgodu) (* summa (expt (+ 1 (bank_proc stavka kapital dneyvgodu)) nperiodov)) ) (write ( complex_proc 2250000.0 4.39 1 181 365 ))
(setq tabl (make-hash-table)) (setf (gethash '001 tabl) '(Charlie Brown)) (setf (gethash '002 tabl) '(Freddie Seal)) (setf (gethash '003 tabl)'(Mama Mongoose)) (maphash #'(lambda (a b) (format t "~a => ~a~%" a b))tabl) ; szóval a maphash-el lehet csináltatni dolgokat a kulcs-érték párokkal\ ; #' ezután kell egy funkció, ami itt lambda, tehát nem kell defun ; a hozzárendelt két változó sorban a kulcs és az érték\ ; itt tehát kiírja a kulcsot, majd ezt: =>, majd kiírja az értéket, s aztán ; ~% - ez azt jelenti, hogy új sor
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more