Linearsearch - LISP

Program:
(defun linsearch(n)
    (format t "~&<<LINEARSEARCH >>")
    (readn n)
    (format t "~&Enter the number to be searched:")
    (setf num (read))
    (setf flag 1)
    (do ((x 0 (+ x 1))) ((= x n))
        (if(= num (aref arr x))
            (setf flag 2)
        )
    )
    (if (= flag 2)
        (format t "Number present...")
        (format t "Number not present...")
    )
)
(defun readn(n)
    (setf arr (make-array n))
    (format t "~&Enter the ~d numbers:" n )
    (dotimes (x n t)
        (setf (aref arr x) (read))
    )
)

Output:
Break 4 [5]> (load 'ls.lsp)
;;  Loading file ls.lsp ...
;;  Loaded file ls.lsp
T
Break 4 [5]> (linsearch 5)
<<LINEARSEARCH >>
Enter the 5 numbers:1
2
3
4
5
Enter the number to be searched:4
Number present...
NIL
Break 4 [5]>

Towers of Hanoi - Programming Environment Lab - LISP

Program:
(defun hanoi(n)
    (dohanoi n 3 1 2)
)

(defun dohanoi(ndisks destination source temp)
    (cond
        ((> ndisks 0) (dohanoi (- ndisks 1) temp source destination)
                (format t "Move the top disk from peg~d to peg~d ~&" source destination)
                (dohanoi (- ndisks 1) destination temp source)
        )
    )
)

Output:
Break 4 [5]> (load 'h.lsp)
;; Loading file h.lsp ...
;; Loaded file h.lsp
T
Break 4 [5]> (hanoi 3)
Move the top disk from peg1 to peg3
Move the top disk from peg1 to peg2
Move the top disk from peg3 to peg2
Move the top disk from peg1 to peg3
Move the top disk from peg2 to peg1
Move the top disk from peg2 to peg3
Move the top disk from peg1 to peg3
NIL
Break 4 [5]>

Generation of Fibonacci Series - Recursion & Iteration -LISP

Program:
;;FIBONACCI SERIES
(defun fib(n)
    (setf a 0 b 1)
    (format t "~&<<<Generation of Fibonacci series with ~D terms>>>~&1.Iterative method~&2.Recursive method~&Enter your choice:" n)
    (setf x (read))
    (cond
        ((= n 1) (print a))
        ((= n 2) (print a)(print b))
        ((> n 2) (if (= x 1)
                (fib1 n))
        (if (= x 2)
             (do ((i 0 (+ i 1))) ((= i n))
                (print (fib2 i)))))
    )
)
(defun fib1(n)
                ;Iterative method
    (print a)(print b)
    (do((i 2 (+ i 1))) ((= i n))
        (setf c (+ a b))
        (print c)
        (setf a b b c))
)
(defun fib2(n)
    (cond
        ((= n 0) 0)
        ((= n 1) 1)
        ((> n 1)(+ (fib2 (- n 1)) (fib2 (- n 2))))
                ;Recursive method
    )
)

Output:
Break 2 [3]> (load 'f.lsp)
;;  Loading file f.lsp ...
;;  Loaded file f.lsp
T
Break 2 [3]> (fib 5)
<<<Generation of Fibonacci series with 5 terms>>>
1.Iterative method
2.Recursive method
Enter your choice:1

0
1
1
2
3
NIL
Break 2 [3]> (fib 5)
<<<Generation of Fibonacci series with 5 terms>>>
1.Iterative method
2.Recursive method
Enter your choice:2

0
1
1
2
3
NIL
Break 2 [3]>

Factorial of a number - LISP

Program:
;;FACTORIAL OF A NUMBER USING RECURSION AND ITERATION
(defun factorial(n)
    (format t "~&<<<Finding factorial of ~D>>>~&1.Iterative method~&2.Recursive method~&Enter your choice:" n)
    (setf x (read))
    (if (= x 1) (format t "factorial = ~D (using iteration)" (fact1 n)))
    (if (= x 2) (format t "factorial = ~D (using recursion)" (fact2 n)))
)
(defun fact1(n) ;;function for iterative method
    (setf f 1)
    (do ((i n (- i 1))) ((= i 1))
        (setf f (* f i))
    )
f)
(defun fact2(n) ;;function for recursive method
    (if (= n 0) 1
        (* n (fact2(- n 1)))
    )
)

Output:
Break 1 [2]> (load 'fact.lsp)
;; Loading file fact.lsp ...
;; Loaded file fact.lsp
T
Break 1 [2]> (factorial 5)
<<<Finding factorial of 5>>>
1.Iterative method
2.Recursive method
Enter your choice:1
factorial = 120 (using iteration)
NIL
Break 1 [2]> (factorial 5)
<<<Finding factorial of 5>>>
1.Iterative method
2.Recursive method
Enter your choice:2
factorial = 120 (using recursion)
NIL                   
Break 1 [2]>

Binary Search - LISP

Program:
;;BINARYSEARCH
(defun binsearch(n)
    (format t "~&<<BINARYSEARCH>>~&")
    (bsreadn n)
    (bssortn n)
    (format t "~&Enter the number to be searched:")
    (setf p (read))
    (setf flag 1)
    (setf first 0)
    (setf last (- n 1))
    (dotimes (x n t)
        (setf mid (floor (+ first last) 2))
        (if (= (aref arr mid) p)(setf flag 0))
        (cond
            ( (>(aref arr mid) p) (setf last (- mid 1)))
            ( (<(aref arr mid) p) (setf first (+ mid 1)))
        )
    )
    (if (= flag 1)
        (format t "Number Not found..."))
    (if (= flag 0)
        (format t "Number found..."))
)
(defun bsreadn(n)
    (setf arr (make-array n))
    (format t "Enter the ~d numbers:" n)
    (dotimes (x n t)
        (setf (aref arr x) (read))
    )
)
                        ;;http://www.2k8618.blogspot.com/
(defun bssortn(n)
    (do (( i 1 (+ i 1))) ((= i n))
        (do ((j 0 (+ j 1))) ((= j (- n 1)))
            (if (> (aref arr j) (aref arr (+ j 1)))
                (bswap j (+ j 1))
            )
        )
    )
    (format t "Sorting....")
    (bprintn n)
)
(defun bprintn(n)
    (dotimes(x n t)
        (print (aref arr x))
    )
)
(defun bswap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

Output:
Break 2 [3]> (load 'bin.lsp)
;;  Loading file bin.lsp ...
;;  Loaded file bin.lsp
T
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:5
4
3
2
1
Sorting....
1
2
3
4
5
Enter the number to be searched:3
Number found...
NIL
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:1
4
2
3
5
Sorting....
1
2
3
4
5
Enter the number to be searched:6
Number Not found...
NIL
Break 2 [3]>
Related Posts Plugin for WordPress, Blogger...