Lisp programming, Study Guides, Projects, Research of Functional Programming

lisp file for lab

Typology: Study Guides, Projects, Research

2014/2015

Uploaded on 05/08/2015

simisimo23
simisimo23 šŸ‡®šŸ‡³

2 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LISP
PROGRAMMING
•Lisp stands for List processing.
•Lisp was invented by John McCarthy in 1958 while he was at the MIT.
•Lisp is second oldest high-level programming languages with a long
history and a distinctive, fully parenthesized syntax.
•Lisp is a Tool to solve some of the most difficult problems in the world
of computing.
•Everything in lisp is either an atom or a list (expression)
•LISP programs are made up of three basic building blocks:
•Atom
•List
•String
Syntax of lisp:-
•An atom is a number or string of contiguous characters. It
includes numbers and special characters.
E.g:- 123008907
*hello*
Block#221
abc123
•A list is a sequence of atoms and/or other lists enclosed in
parentheses.
E.g:- (i am a list)
(a ( a b c) d e f g h)
(father tom ( susan bill joe))
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Lisp programming and more Study Guides, Projects, Research Functional Programming in PDF only on Docsity!

LISP

PROGRAMMING

  • Lisp stands for List processing.
  • (^) Lisp was invented by John McCarthy in 1958 while he was at the MIT.
  • Lisp is second oldest high-level programming languages with a long history and a distinctive, fully parenthesized syntax.
  • Lisp is a Tool to solve some of the most difficult problems in the world of computing.
  • Everything in lisp is either an atom or a list (expression)
  • LISP programs are made up of three basic building blocks:
  • Atom
  • List
  • String

Syntax of lisp:-

  • An atom is a number or string of contiguous characters. It includes numbers and special characters.

E.g:- 123008907 hello Block# abc

  • A list is a sequence of atoms and/or other lists enclosed in parentheses.

E.g:- (i am a list)

(a ( a b c) d e f g h)

(father tom ( susan bill joe))

(sun mon tue wed thur fri sat)

( )

  • A string is a group of characters enclosed in double quotation marks.

E.g:- " I am a string"

"a ba c d efg #$%^&!"

"Please enter the following details:" "Hello from 'Tutorials Point!"

  • Example of lisp:- (DEFUN HELLO () "HELLO WORLDā€)

Functions

Defun is used to define functions.

  1. (defun square (x)

_( x x))_*

  1. (defun add (x y)

(+ x y))

Advantages of LISP

  • Common Lisp is
    • a general-purpose programming language and an AI language
    • interactive
  • Common Lisp programs are
    • easy to test (interactive)
    • easy to maintain (depending on programming style)
    • portable across hardware/OS platforms and implementations (there is a standard for the language and the library functions)

Areas of Application

program of factorial

  • math operators: (- n 1) is prefix notation equivalent to n-1 in infix notation;
  • (^) comparison operators: (= n 0) evaluates to T if n is zero, and to nil (used as false) otherwise;
  • conditional operator if: Lisp expressions are evaluated using brackets, so they can be written in several lines;
  • function definition using defun;
  • Common Lisp macro loop;
  • Format specifiers in format: ~D corresponds to printing an integer, and ~% is end-of-line.

Program of area of circle

Program to find max of three

numbers

  • Define a function max3 with 3 parameters a, b, c.
  • condition clause begins:-
    • first condition check whether a>b or not, if yes
    • Check whether a>c, if yes then a is largest of three.
  • The procedures max and min return the maximum and minimum respectively of the number arguments supplied to them. Any number of arguments can be so supplied.

(max 1 3 4 2 3) => 4

(min 1 3 4 2 3) => 1

  • Given a list of elements, find both the maximum element and the minimum element
  • Obvious solution:
    1. Consider first element to be max
    2. Consider first element to be min
    3. (^) Scan linearly from 2nd to last, and update if something larger then max or if something smaller than min

Program of bubble sort

  • The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values
  • The Fibonacci numbers are the integer sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, in which each item is formed by adding the previous two. The sequence can be defined recursively by
  • Fibonacci numbers is a numerical sequence, in which first two elements are equal to 1, and each remaining number is equal to the sum of the previous two: F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2).
  • The logic behind it is that we need to know the two previous numbers to generate the next.

Program of structure

(defstruct student name rollno class )

  • (^) The above declaration creates a student structure with three named components.
  • It defines four functions named name, class, rollno which will take one argument, a student structure, and will return the fields name, class,rollno of the student object. These functions are called the access functions.

Program of array creation

using the accessor function aref. As with hash tables, we can set individual items in the array using setf.

  1. CREATE AN ARRAY:-

(setq my-array (make-array (list 3 3))) #2A((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))

  1. SET VALUES IN ARRAY:-

(setf (aref my-array 0 0) "Dave") "Daveā€ (setf (aref my-array 0 1) "John") "John"

  1. USING OF ACCESSOR FUNCTION:-

(aref my-array 0 0) "Dave" (aref my-array 0 1) "John"

program of FOR LOOP