Rahul Kumar Saurabh

Showing posts with label LISP Program. Show all posts
Showing posts with label LISP Program. Show all posts

A Recursive LISP function which takes one argument as a list and return reverse of the list.

Programs in lisp with output

Define a Recursive LISP function which takes one argument as a list and return reverse of the list.

SOFTWARE USED:  LispWorks 6.1
THEORY:-
Assuming this is about Common Lisp, there is a function  one argument as a list and return reverse of the list.  If you use this function which applies a given function to each element of a list and return reverse of the list.

SOURCE CODE ( IF INCLUDED )
(defun list-append (L1 L2)
  "Append L1 by L2."
  (if (null L1)
      L2
    (cons (first L1) (list-append (rest L1) L2))))

LIST-APPEND

(defun show-list-reverse (L)
  "Create a new list containing the elements of L in reversed order."
  (if (null L)
      nil
    (list-append (show-list-reverse (rest L))
                 (list (first L)))))

SHOW-LIST-REVERSE
(show-list-reverse '(1 2 3 4))

(4 3 2 1)


 RESULT:-



CONCLUSION:-
We learned about writing the function to which takes one argument as a list and return reverse of the list..



A Recursive LISP function which appends two lists together.


Programs in lisp with output

Define a Recursive LISP function which appends two lists together.

SOFTWARE USED:  LispWorks 6.1
THEORY:-
Assuming this is about Common Lisp, there is a function which appends two lists. If you use this function which applies a given function two list as parameters, returns first list with the element of the second list appended.

SOURCE CODE ( IF INCLUDED )
(defun list-append (L1 L2)
  "Append L1 by L2."
  (if (null L1)
      L2
    (cons (first L1) (list-append (rest L1) L2))))
LIST-APPEND
> (list-append '(a  b c) '( c d e))
(A B C C D E)
> (list-append '(4 2)'(2 3))
(4 2 2 3)



 RESULT:-

CONCLUSION:-
We learned about writing the recursive LISP function which appends two lists together

A Recursive LISP function which takes one argument as a list and return list except last element of the list.

           Programs in lisp with output



SOFTWARE USED:  LispWorks 6.1
THEORY:-
Assuming this is about Common Lisp, there is a function  which return list except last element of the list containing the except last item of a list.  If you use this function which applies a given function to each element of a list and returns the concatenated results.

SOURCE CODE ( IF INCLUDED )
(defun except_last_element(any_xyz_list)
(reverse(rest(reverse any_xyz_list))))

EXCEPT_LAST_ELEMENT

(except_last_element' (r a h u l k))

(R A H U L)


 RESULT:-



CONCLUSION:-
We learned about writing the function to which takes one argument as a list and return list except last element of the list. 

A Recursive LISP function which takes one argument as a list and return last element of The list.

  
           Programs in lisp with output


SOFTWARE USED:  LispWorks 6.1

THEORY:-
Assuming this is about Common Lisp, there is a function which returns a list containing the last item of a list.  If you use this function which applies a given function to each element of a list and returns the concatenated results, you'll get what you want.


SOURCE CODE ( IF INCLUDED )

(defun last_element(any_xyz_list)
(first(reverse any_xyz_list)))

LAST_ELEMENT

(last_element '(p q r s))

s


RESULT:-


CONCLUSION:-

We learned about writing the function to evaluate takes one argument as a list and return last element of The list.

A Recursive LISP function to compute factorial of a given number.

          Programs in lisp with output


SOFTWARE USED:  LispWorks 6.1

THEORY:-
Factorial
The factorial of a non-negative integer n , denoted by n! , is the product of all positive integers less than or equal to n.


Example-
                       4! = 4 * 3 * 2 * 1 = 24
similarly         6! = 6 * 5 * 4 * 3 * 2 * 1 =  720
The value of 0! is 1, according to the convention for an empty product.

SOURCE CODE ( IF INCLUDED )
(defun factorial (n)
  
(if (= n 1)
 1
 (* n (factorial (- n 1)))))

(factorial 4)
24

(factorial 6)
720

RESULT:-



CONCLUSION:-
We learned about writing the function to compute factorial of a given number.


A Recursive LISP function to solve Ackermann’s Function.

   Programs in lisp with output


SOFTWARE USED:- LispWorks 6.1.

THEORY:-
ACKERMANN's  FUNCTION
The Ackermann function, named after Wilhelm Ackermann  is one of the simplest and earliest-discovered examples of a total computable function that is not primitive recursive. All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are primitive recursive.
After Ackermann's publication of his function (which had three nonnegative integer arguments), many authors modified it to suit various purposes, so that today "the Ackermann function" may refer to any of numerous variants of the original function. One common version, the two-argument Ackermann–Peter function, is defined as follows for nonnegative integers m and n:-

SOURCE CODE ( IF INCLUDED )
(defun ackermann (m n)
(cond ((= m 0) (+ n 1))
      ((= m 1) (+ n 2))
      ((= m 2) (+ 3 (* n 2)))
      ((= m 3) (+ 5 (* 8 (- (expt 2 n) 1))))
      (t (cond ((= n 0) (ackermann (- m 1) 1))
               (t (ackermann (- m 1) (ackermann m (- n 1))))
         ))
))

 (ackermann 2 3 )
 9

RESULT:-


CONCLUSION:-

We defined a LISP function to compute Ackermann's Function and test it on various values.

Define a LISP function to compute difference of squares


            Programs in lisp with output


SOFTWARE USED:   LispWorks 6.1

THEORY:-
DIFFERENCE OF SQUARE
Difference of square means by subtracting the squares of two numbers.
Example
                Difference of square of x,y= ( x * x ) - ( y * y )
Now in prefix expression it should be written as
( - ( * x x ) ( * y y ) )


SOURCE CODE ( IF INCLUDED )
(defun diffsqr(x y)
 ( - ( *  x  x ) ( *  y  y ) )
( diffsqr 4 3 )
7

RESULT:-



CONCLUSION:-

We  defined a LISP function to compute difference of square and test it on various values.

Define a LISP function to compute sum of squares.

 Programs in lisp with output

Q. Define a LISP function to compute sum of squares.

SOFTWARE USED:  LispWorks 6.1


THEORY:-

SUM OF SQUARE
Sum of square means by adding the squares of two numbers.
Example
                Sum of Square of x ,y= ( x * x ) + ( y * y )
Now in prefix expression it should be written as
( + ( * x x ) ( * y y ) )

SOURCE CODE ( IF INCLUDED )
(defun sumsqr(x y)

( + ( * x x )( *  y  y )))

 (sumsqr 2 3 )

13               

RESULT:-



CONCLUSION:-
We defined a LISP function to compute sum of square and test it on various values.

A function in LISP that evaluate a fully parenthesized infix arithmetic expression.

Programs in lisp with output

Q. Write a function in LISP that evaluate a fully parenthesized infix arithmetic  expression.


SOFTWARE USED:  LispWorks 6.1

THEORY:-

INFIX   EXPRESSIONS
Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands.
Example-
                       ( 2 + 3 )=5
PREFIX   EXPRESSIONS
Prefix notation is written in the form as the operator is placed before the operands.
Example-
                      ( + 2 3 )=5
In LISP programming we always write our code in prefix expression.
Example-
                      ( - 9 5 ) =4

SOURCE CODE ( IF INCLUDED )
( + 6 5 )
11

RESULT:-



CONCLUSION:-
We learned about writing the function to evaluate a fully parenthesized infix arithmetic expression.