SlideShare a Scribd company logo
1 of 65
Unit 4
LISP
Introduction; Syntax and numeric functions; basic list manipulation functions;
functions predicates and conditionals; input , output and local variables ; property
lists and arrays.
LISP- List Processing language
Lisp is a programming language that has an overall style that is organized around expressions
and functions.
Every Lisp procedure is a function, and when called, it returns a data object as its value. It is also
commonly referred to as “functions” even though they may have side effects.
Lisp is the second-oldest high-level programming language in the world which is invented by
John McCarthy in the year 1958 at the Massachusetts Institute of Technology.
History
Features of LISP Programming Language:
It is a machine-independent language
It uses iterative design methodology and is easily extensible
It allows us to create and update the programs and applications dynamically.
It provides high-level debugging.
It supports object-oriented programming.
It supports all kinds of data types like objects, structures, lists, vectors, adjustable arrays, set,
trees,hash-tables, and symbols.
It is an expression-based language
It can support different decision-making statements like if, when,case, and cond
It will also support different iterating statements like do, loop,loopfor, dotimes and dolist.
It will support input and output functions
By using lisp we can also create our own functions
More on LISP
LISP expressions are case-insensitive
The basic numeric operations in LISP are +, -, *, and /
LISP represents a function call f(x) as (f x), for example cos(45) is written as (cos 45)
Lisp has a different syntax than C like languages (C++, Java, C#)
Uses parenthesis extensively - ( and )
Uses prefix notation For example the expression to add 1 and 2 would be (+ 1 2) in Lisp.
LISP tries to evaluate everything, including the arguments of a function. Only three types of
elements are constants and always return their own value
◦ Numbers
◦ The letter t, that stands for logical true.
◦ The value nil, that stands for logical false, as well as an empty list.
Prefix Notation in LISP
In prefix notation, operators are written before their operands. For example, the expression,
a * ( b + c ) / d
will be written as −
(/ (* a (+ b c) ) d)
Let us take another example, let us write code for converting Fahrenheit temp of 60 degree F to the
centigrade scale −
The mathematical expression for this conversion will be −
(60 * 9 / 5) + 32
Create a source code file named main.lisp and type the following code in it.
(write(+ (* (/ 9 5) 60) 32))
?? Output: ???
Basic Lisp Syntax
LISP - Program Structure
LISP expressions are called symbolic expressions or s-expressions. The s-expressions are composed of three valid
objects,
a. atoms,
b. lists
c. strings.
Any s-expression is a valid program.
LISP programs run either on an interpreter or as compiled code.
The interpreter checks the source code in a repeated loop, which is also called the read-evaluate-print loop (REPL).
It reads the program code, evaluates it, and prints the values returned by the program.
Basic Building Blocks in LISP
LISP programs are made up of three basic building blocks −
atom
list
string
An atom is a number or string of contiguous characters. It includes numbers and special
characters.
◦ hello-from-kjc
◦ name
◦ 123008907
◦ *hello*
◦ Block#221
◦ abc123
Building Blocks of Lisp
A Simple Program
Let us write an s-expression to find the sum of three numbers 4, 3 and 1.
Using Interpreter:
To do this, we can type at the interpreter prompt
(+ 4 3 1)
LISP returns the result − 8
Using Compiler:
create a LISP source code file named sumProg.lisp and type the following code in it.
(write (+ 4 3 1))
LISP returns the result − 8
The 'Hello World' Program
(write-line "Hello World")
(write-Line "I am at ‘kjc'! Learning LISP")
(write-Line "I AM AT ‘KJC'! LEARNING LISP")
Since its Case insensitive both line and Line are same…
Adding Comments
The semicolon symbol (;) is used for indicating a comment line.
A Lisp comment begins with a semi-colon.
; This is a Lisp comments - ignored by the interpreter.
(print "Hello World")
; this is an example of comment
; the following statement prints addition of 2 and 3
(print (+ 2 3))
multiple comment through #| … |#
Naming Conventions in LISP
The naming Conventions mean the way we are declaring variables in a program. It includes the
variable names and syntax formats
A variable can contain any number of alphanumeric characters other than whitespace, open and
closing parentheses, double and single quotes, backslash, comma, colon, semicolon and vertical
bar.
A name can have digits but not entirely made of digits, because then it would be read as a
number.
A name can have periods, but can't be made entirely of periods.
Acceptable: hello,akshit, etc
Not Acceptable: hell()0,sat{ akshit{,,,,,,,,etc
Naming Conventions in LISP.
;acceptable naming conventions
(write-line "akshit")
(terpri) ; Terminate print line
(write-line "akshit99")
(terpri)
(write-line "hello geeks")
Use of Single Quotation Mark
• LISP evaluates everything including the function arguments and list members.
• If we don't want them evaluated or treated as function calls.
• we need to precede the atom or the list with a single quotation mark.
• Create a file named main.lisp and type the following code into it
(write-line "single quote used, it inhibits evaluation")
(write '(* 2 3))
(write-line " ") ;new line
(write-line "single quote not used, so expression evaluated")
(write (* 2 3))
Symbol
Symbols are lisp data objects and every type of symbol object has a name called its print name.
Symbol names may contain any combination of letters and numbers, plus some special characters
such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter
characters like parenthesis or space.
Examples of symbols :
Banana
age
year-of-birth
123%$
/home/user/work
b^2-a*c
Integers Symbols
A sequence of numbers from 0 to 9.
eg: +7 and 7 are integers.
A sequence of letters, digits and, other permissible characters.
eg: + , – , * are all symbols .
Special symbols T and NIL:
T : Truth, ” yes “
NIL : False, “no”
DataTypes
In LISP, variables are not typed, but data objects are.
A data type is basically a collection of values that have the same type. We can also say that in
lisp, every object belongs to a data type. An object in lisp is a data that is used and manipulated
by the lisp code.
LISP data types can be categorized as.
◦ Scalar types − for example, number types, characters, symbols ,float etc. – used to store single value
◦ Data structures − for example, array lists, vectors, bit-vectors, and strings.-used to store multiple values
Any variable can take any LISP object as its value, unless you have declared it explicitly.
commonly used data types in LISP.
1. Numbers: Numbers are used for storing the integer and floating-point values. Numbers are
represented by the symbol number. Lisp has two types of numbers: integer and floating-point. A
floating-point number is a number with a decimal point. An integer is a whole number without a
decimal point. Lisp also provides Cartesian complex numbers.
Type Specifiers in LISP
Type specifiers are system-defined symbols for data types.
Other than these system-defined types, you can create your own data types. When a structure type is
defined using defstruct function, the name of the structure type becomes a valid type symbol.
Assignment statement
Create new source code file named main.lisp and type the following code in it.
(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print x)
(print y)
(print n)
(print ch)
(print bg)
(print r)
list manipulation functions in lisp
•In Lisp, lists are a fundamental data structure, and there are many built-in
functions for manipulating them
• cons - creates a new list by adding an element to the front of an existing list.
• (cons 1 '(2 3 4)) ; => (1 2 3 4)
• car - returns the first element of a list.
• (car '(1 2 3 4)) ; => 1
• cdr - returns all elements of a list except the first one.
• (cdr '(1 2 3 4)) ; => (2 3 4)
• append - appends two or more lists together.
• (append '(1 2) '(3 4)) ; => (1 2 3 4)
• nth - returns the nth element of a list.
• (nth 2 '(1 2 3 4)) ; => 3
• length - returns the number of elements in a list.
• (length '(1 2 3 4)) ; => 4
• reverse - reverses the order of the elements in a list.
• (reverse '(1 2 3 4)) ; => (4 3 2 1)
• member - checks if an element is a member of a list.
• (member 3 '(1 2 3 4)) ; => (3 )
• remove - removes all occurrences of an element from a list.
• (remove 3 '(1 2 3 4 3)) ; => (1 2 4)
• subseq - returns a subsequence of a list starting at a specified index and
ending at another specified index.
• (subseq '(1 2 3 4) 1 3) ; => (2 3)
• (write (list 1 2)) --> ( 1 2)
• (terpri)
Functions, predicates, and conditionals
•A function is a group of statements that together perform a task.
•In Lisp, functions are defined using the defun keyword.
Defining Functions in LISP
• The macro named defun is used for defining functions. The defun macro needs three
arguments −
• Name of the function
• Parameters of the function
• Body of the function
Functions
•The general syntax for defining a function is:
(defun function-name (parameters)
"Documentation string"
body-of-function
)
Example:
(defun add (n1 n2)
"Adds two numbers"
(+ n1 n2)
)
(write(add (10 20))
(defun averagenum (n1 n2 n3 n4)
(/ ( + n1 n2 n3 n4) 4)
)
(write(averagenum 10 20 30 40))
(defun area-circle(rad)
;Calculates area of a circle with given radius
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~5f" (* 3.14 rad rad))
)
(area-circle 10)
output:
Radius: 10.0
Area: 314.1592
Predicates
Predicates are functions that test their arguments for
some specific conditions and returns nil if the condition
is false, or some non-nil value is the condition is true.
Commonly used predicates
atom
It takes one argument and returns t if the argument is an
atom or nil if otherwise.
equal
It takes two arguments and returns t if they are
structurally equal or nil otherwise.
evenp
It takes one numeric argument and returns t if the
argument is even number or nil if otherwise.
oddp
It takes one numeric argument and returns t if the argument is odd
number or nil if otherwise.
zerop
It takes one numeric argument and returns t if the argument is zero
or nil if otherwise.
listp
It takes one argument and returns t if the argument evaluates to a
list otherwise it returns nil.
greaterp
It takes one or more argument and returns t if either there is a single
argument or the arguments are successively larger from left to right,
or nil if otherwise.
lessp
It takes one or more argument and returns t if either there is a single
argument or the arguments are successively smaller from left to
right, or nil if otherwise.
(write (atom 'abcd))
(terpri)
(write (equal 'a 'b))
(terpri)
(write (evenp 10))
(terpri)
(write (evenp 7 ))
(terpri)
(write (oddp 7 ))
(terpri)
(write (zerop 0.0000000001))
(terpri)
(write (eq 3 3.0 ))
(terpri)
(write (equal 3 3.0 ))
(terpri)
(write (equal 3 3 ))
(terpri)
(write (null nil ))
T
NIL
TNIL
T
NIL
NIL
NIL
T
T
(defun factorial (num)
(cond ((zerop num) 1)
(t ( * num (factorial (- num 1))))
)
)
(setq n 6)
(format t "~% Factorial ~d is: ~d" n (factorial n))
Factorial 6 is: 720
Decision-making statements
• Decision making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.
• Following is the general form of a typical decision making structure
found in most of the programming languages −
Conditional
cond
(cond (test1 action1)
(test2 action2)
...
(testn actionn))
(setq a 10)
(cond ((> a 20)
(format t "~% a is greater than 20"))
(t (format t "~% value of a is ~d " a)))
(value of a is 10
if
(if (test-clause) (action1) (action2)) (setq a 10)
(if (> a 20)
(format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)
(value of a is 10
if
a is less than 20
value of a is 10
(setq a 10)
(if (> a 20)
then (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)
if
a is greater than 20
value of a is 100
(setq a 100)
(if (> a 20)
(format t "~% a is greater than 20")
(format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)
when
(when (test-clause) (<action1) ) (setq a 100)
(when (> a 20)
(format t "~% a is greater than 20"))
(format t "~% value of a is ~d " a)
a is greater than 20
value of a is 100
case
(case (keyform)
((key1) (action1 action2 ...) )
((key2) (action1 action2 ...) )
...
((keyn) (action1 action2 ...) ))
(setq day 4)
(case day
(1 (format t "~% Monday"))
(2 (format t "~% Tuesday"))
(3 (format t "~% Wednesday"))
(4 (format t "~% Thursday"))
(5 (format t "~% Friday"))
(6 (format t "~% Saturday"))
(7 (format t "~% Sunday")))
Thursday
Example
(setq a 10)
(cond ((> a 20)
(format t "~% a is greater than 20"))
(t (format t "~% value of a is ~d " a)))
OUTPUT:
value of a is 10
Example 2
The if clause can be followed by an optional then clause.
(setq a 10)
(if (> a 20)
then (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)
OUTPUT:
a is less than 20
value of a is 10
Example 3
You can also create an if-then-else type statement using the if clause.
(setq a 100)
(if (> a 20)
(format t "~% a is greater than 20")
(format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)
OUTPUT:
value of a is 100
Variable Declaration
(defvar x 10)
(defvar y 34.567)
(defvar ch nil)
(defvar n 123.78)
(defvar bg 11.0e+4)
(defvar r 124/2)
(print (type-of x))
(print (type-of y))
(print (type-of n))
(print (type-of ch))
(print (type-of bg))
(print (type-of r))
Global Variables
Global variables have permanent values throughout the LISP system and remain in effect until a
new value is specified.
Global variables are generally declared using the defvar construct.
(defvar x 234)
(write x)
there is no type declaration for variables in LISP, you directly specify a value for a symbol with
the setq construct.
For Example
(setq x 10)
symbol-value
The symbol-value function allows you to extract the value stored at the symbol storage
EXAMPLE
(setq x 10)
(setq y 20)
(format t "x = ~2d y = ~2d ~%" x y)
(setq x 100)
(setq y 200)
(format t "x = ~2d y = ~2d" x y)
~% allows the value to be printed in next line.
Local Variable
Local variables are defined within a given procedure. The parameters named as arguments within a function definition are also local variables.
Local variables are accessible only within the respective function.
Like the global variables, local variables can also be created using the setq construct.
There are two other constructs - let and prog for creating local variables.
The let construct has the following syntax.
(let ((var1 val1) (var2 val2).. (varn valn))<s-expressions>)
Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the respective variables.
When let is executed, each variable is assigned the respective value and lastly the s-expression is evaluated. The value of the last expression
evaluated is returned.
If you don't include an initial value for a variable, it is assigned to nil.
Example- let construct
(let ((x 'a) (y 'b)(z 'c))
(format t "x = ~a y = ~a z = ~a" x y z))
Prog construct
The prog construct also has the list of local variables as its first argument, which is followed by
the body of the prog, and any number of s-expressions.
The prog function executes the list of s-expressions in sequence and returns nil unless it
encounters a function call named return. Then the argument of the return function is evaluated
and returned
(prog ((x '(a b c))(y '(1 2 3))(z '(p q 10)))
(format t "x = ~a y = ~a z = ~a" x y z))
Arrays
LISP allows you to define single or multiple-dimension arrays using the make-
array function. An array can store any LISP object as its elements.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
The number of dimensions of an array is called its rank.
In LISP, an array element is specified by a sequence of non-negative integer
indices. The length of the sequence must equal the rank of the array. Indexing
starts from zero.
For example, to create an array with 10- cells, named my-array,
we can write −
(setf my-array (make-array '(10)))
The aref function allows accessing the contents of the cells. It
takes two arguments, the name of the array and the index
value.
For example, to access the content of the tenth cell, we write −
(aref my-array 9)
Create a new source code file named main.lisp and type the
following code in it.
Live Demo
(write (setf my-array (make-array '(10))))
(terpri)
(setf (aref my-array 0) 25)
(setf (aref my-array 1) 23)
(setf (aref my-array 2) 45)
(setf (aref my-array 3) 10)
(setf (aref my-array 4) 20)
(setf (aref my-array 5) 17)
(setf (aref my-array 6) 25)
(setf (aref my-array 7) 19)
(setf (aref my-array 8) 67)
(setf (aref my-array 9) 30)
(write my-array)
END OF UNIT 4
Looping not in syllabus
loop
(loop (s-expressions)) (setq a 10)
(loop
(setq a (+ a 1))
(write a)
(terpri)
(when (> a 15) (return a))
)
11
12
13
14
15
16
loop for
(loop for loop-variable in <a list>
do (action)
)
(loop for loop-variable from value1 to
value2
do (action)
)
(loop for x in '(Hello World)
do (format t " ~s" x)
)
HELLO WORLD
loop for
(loop for loop-variable in <a list>
do (action)
)
(loop for loop-variable from value1 to
value2
do (action)
)
(loop for a from 10 to 20
do (print a)
)
10
11
12
13
14
15
16
17
18
19
20
do
(do ((variable1 value1 updated-value1)
(variable2 value2 updated-value2)
(variable3 value3 updated-value3)
...)
(test return-value)
(s-expressions)
)
(do ((x 0 (+ 2 x))
(y 20 ( - y 2)))
((= x y)(- x y))
(format t "~% x = ~d y = ~d" x y)
)
x = 0 y = 20
x = 2 y = 18
x = 4 y = 16
x = 6 y = 14
x = 8 y = 12
dotimes
(dotimes (n 11)
(print n) (prin1 (* n n))
)
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
dotimes
(dotimes (n 5)
(print n) (prin1 (+ n n))
)
0 0
1 2
2 4
3 6
4 8
dolist
(dolist (n '(1 2 3 4 5 6 7 8 9))
(format t "~% Number: ~d Square: ~d" n (* n n))
)
Number: 1 Square: 1
Number: 2 Square: 4
Number: 3 Square: 9
Number: 4 Square: 16
Number: 5 Square: 25
Number: 6 Square: 36
Number: 7 Square: 49
Number: 8 Square: 64
Number: 9 Square: 81

More Related Content

Similar to AI UNIT-4 Final (2).pptx

Similar to AI UNIT-4 Final (2).pptx (20)

Introduction to Programming in LISP
Introduction to Programming in LISPIntroduction to Programming in LISP
Introduction to Programming in LISP
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
Getting started with c++.pptx
Getting started with c++.pptxGetting started with c++.pptx
Getting started with c++.pptx
 
Python Basics
Python BasicsPython Basics
Python Basics
 
LISP.ppt
LISP.pptLISP.ppt
LISP.ppt
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming Language
 
Learning ObjectivesGain some experience using dynamic data structu.docx
Learning ObjectivesGain some experience using dynamic data structu.docxLearning ObjectivesGain some experience using dynamic data structu.docx
Learning ObjectivesGain some experience using dynamic data structu.docx
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
Lisp
LispLisp
Lisp
 

More from prakashvs7

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptxprakashvs7
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxprakashvs7
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxprakashvs7
 
unit 5_Real time Data Analysis vsp.pptx
unit 5_Real time Data Analysis  vsp.pptxunit 5_Real time Data Analysis  vsp.pptx
unit 5_Real time Data Analysis vsp.pptxprakashvs7
 
final Unit 1-1.pdf
final Unit 1-1.pdffinal Unit 1-1.pdf
final Unit 1-1.pdfprakashvs7
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxprakashvs7
 
AI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxAI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxprakashvs7
 
AI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxAI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxprakashvs7
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxprakashvs7
 

More from prakashvs7 (15)

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptx
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptx
 
unit 5_Real time Data Analysis vsp.pptx
unit 5_Real time Data Analysis  vsp.pptxunit 5_Real time Data Analysis  vsp.pptx
unit 5_Real time Data Analysis vsp.pptx
 
unit 4-1.pptx
unit 4-1.pptxunit 4-1.pptx
unit 4-1.pptx
 
unit 3.ppt
unit 3.pptunit 3.ppt
unit 3.ppt
 
final Unit 1-1.pdf
final Unit 1-1.pdffinal Unit 1-1.pdf
final Unit 1-1.pdf
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
AI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxAI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptx
 
AI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxAI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
 
Php unit i
Php unit i Php unit i
Php unit i
 
The process
The processThe process
The process
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

AI UNIT-4 Final (2).pptx

  • 1. Unit 4 LISP Introduction; Syntax and numeric functions; basic list manipulation functions; functions predicates and conditionals; input , output and local variables ; property lists and arrays.
  • 2. LISP- List Processing language Lisp is a programming language that has an overall style that is organized around expressions and functions. Every Lisp procedure is a function, and when called, it returns a data object as its value. It is also commonly referred to as “functions” even though they may have side effects. Lisp is the second-oldest high-level programming language in the world which is invented by John McCarthy in the year 1958 at the Massachusetts Institute of Technology.
  • 4. Features of LISP Programming Language: It is a machine-independent language It uses iterative design methodology and is easily extensible It allows us to create and update the programs and applications dynamically. It provides high-level debugging. It supports object-oriented programming. It supports all kinds of data types like objects, structures, lists, vectors, adjustable arrays, set, trees,hash-tables, and symbols. It is an expression-based language It can support different decision-making statements like if, when,case, and cond It will also support different iterating statements like do, loop,loopfor, dotimes and dolist. It will support input and output functions By using lisp we can also create our own functions
  • 5. More on LISP LISP expressions are case-insensitive The basic numeric operations in LISP are +, -, *, and / LISP represents a function call f(x) as (f x), for example cos(45) is written as (cos 45) Lisp has a different syntax than C like languages (C++, Java, C#) Uses parenthesis extensively - ( and ) Uses prefix notation For example the expression to add 1 and 2 would be (+ 1 2) in Lisp. LISP tries to evaluate everything, including the arguments of a function. Only three types of elements are constants and always return their own value ◦ Numbers ◦ The letter t, that stands for logical true. ◦ The value nil, that stands for logical false, as well as an empty list.
  • 6. Prefix Notation in LISP In prefix notation, operators are written before their operands. For example, the expression, a * ( b + c ) / d will be written as − (/ (* a (+ b c) ) d) Let us take another example, let us write code for converting Fahrenheit temp of 60 degree F to the centigrade scale − The mathematical expression for this conversion will be − (60 * 9 / 5) + 32 Create a source code file named main.lisp and type the following code in it. (write(+ (* (/ 9 5) 60) 32)) ?? Output: ???
  • 8. LISP - Program Structure LISP expressions are called symbolic expressions or s-expressions. The s-expressions are composed of three valid objects, a. atoms, b. lists c. strings. Any s-expression is a valid program. LISP programs run either on an interpreter or as compiled code. The interpreter checks the source code in a repeated loop, which is also called the read-evaluate-print loop (REPL). It reads the program code, evaluates it, and prints the values returned by the program.
  • 9. Basic Building Blocks in LISP LISP programs are made up of three basic building blocks − atom list string An atom is a number or string of contiguous characters. It includes numbers and special characters. ◦ hello-from-kjc ◦ name ◦ 123008907 ◦ *hello* ◦ Block#221 ◦ abc123
  • 11. A Simple Program Let us write an s-expression to find the sum of three numbers 4, 3 and 1. Using Interpreter: To do this, we can type at the interpreter prompt (+ 4 3 1) LISP returns the result − 8 Using Compiler: create a LISP source code file named sumProg.lisp and type the following code in it. (write (+ 4 3 1)) LISP returns the result − 8
  • 12. The 'Hello World' Program (write-line "Hello World") (write-Line "I am at ‘kjc'! Learning LISP") (write-Line "I AM AT ‘KJC'! LEARNING LISP") Since its Case insensitive both line and Line are same…
  • 13. Adding Comments The semicolon symbol (;) is used for indicating a comment line. A Lisp comment begins with a semi-colon. ; This is a Lisp comments - ignored by the interpreter. (print "Hello World") ; this is an example of comment ; the following statement prints addition of 2 and 3 (print (+ 2 3))
  • 14.
  • 16. Naming Conventions in LISP The naming Conventions mean the way we are declaring variables in a program. It includes the variable names and syntax formats A variable can contain any number of alphanumeric characters other than whitespace, open and closing parentheses, double and single quotes, backslash, comma, colon, semicolon and vertical bar. A name can have digits but not entirely made of digits, because then it would be read as a number. A name can have periods, but can't be made entirely of periods. Acceptable: hello,akshit, etc Not Acceptable: hell()0,sat{ akshit{,,,,,,,,etc
  • 17. Naming Conventions in LISP. ;acceptable naming conventions (write-line "akshit") (terpri) ; Terminate print line (write-line "akshit99") (terpri) (write-line "hello geeks")
  • 18. Use of Single Quotation Mark • LISP evaluates everything including the function arguments and list members. • If we don't want them evaluated or treated as function calls. • we need to precede the atom or the list with a single quotation mark. • Create a file named main.lisp and type the following code into it (write-line "single quote used, it inhibits evaluation") (write '(* 2 3)) (write-line " ") ;new line (write-line "single quote not used, so expression evaluated") (write (* 2 3))
  • 19. Symbol Symbols are lisp data objects and every type of symbol object has a name called its print name. Symbol names may contain any combination of letters and numbers, plus some special characters such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter characters like parenthesis or space. Examples of symbols : Banana age year-of-birth 123%$ /home/user/work b^2-a*c
  • 20. Integers Symbols A sequence of numbers from 0 to 9. eg: +7 and 7 are integers. A sequence of letters, digits and, other permissible characters. eg: + , – , * are all symbols . Special symbols T and NIL: T : Truth, ” yes “ NIL : False, “no”
  • 21. DataTypes In LISP, variables are not typed, but data objects are. A data type is basically a collection of values that have the same type. We can also say that in lisp, every object belongs to a data type. An object in lisp is a data that is used and manipulated by the lisp code. LISP data types can be categorized as. ◦ Scalar types − for example, number types, characters, symbols ,float etc. – used to store single value ◦ Data structures − for example, array lists, vectors, bit-vectors, and strings.-used to store multiple values Any variable can take any LISP object as its value, unless you have declared it explicitly.
  • 22. commonly used data types in LISP. 1. Numbers: Numbers are used for storing the integer and floating-point values. Numbers are represented by the symbol number. Lisp has two types of numbers: integer and floating-point. A floating-point number is a number with a decimal point. An integer is a whole number without a decimal point. Lisp also provides Cartesian complex numbers.
  • 23.
  • 24.
  • 25. Type Specifiers in LISP Type specifiers are system-defined symbols for data types. Other than these system-defined types, you can create your own data types. When a structure type is defined using defstruct function, the name of the structure type becomes a valid type symbol.
  • 26. Assignment statement Create new source code file named main.lisp and type the following code in it. (setq x 10) (setq y 34.567) (setq ch nil) (setq n 123.78) (setq bg 11.0e+4) (setq r 124/2) (print x) (print y) (print n) (print ch) (print bg) (print r)
  • 27. list manipulation functions in lisp •In Lisp, lists are a fundamental data structure, and there are many built-in functions for manipulating them • cons - creates a new list by adding an element to the front of an existing list. • (cons 1 '(2 3 4)) ; => (1 2 3 4) • car - returns the first element of a list. • (car '(1 2 3 4)) ; => 1 • cdr - returns all elements of a list except the first one. • (cdr '(1 2 3 4)) ; => (2 3 4) • append - appends two or more lists together. • (append '(1 2) '(3 4)) ; => (1 2 3 4) • nth - returns the nth element of a list. • (nth 2 '(1 2 3 4)) ; => 3
  • 28. • length - returns the number of elements in a list. • (length '(1 2 3 4)) ; => 4 • reverse - reverses the order of the elements in a list. • (reverse '(1 2 3 4)) ; => (4 3 2 1) • member - checks if an element is a member of a list. • (member 3 '(1 2 3 4)) ; => (3 ) • remove - removes all occurrences of an element from a list. • (remove 3 '(1 2 3 4 3)) ; => (1 2 4) • subseq - returns a subsequence of a list starting at a specified index and ending at another specified index. • (subseq '(1 2 3 4) 1 3) ; => (2 3) • (write (list 1 2)) --> ( 1 2) • (terpri)
  • 29. Functions, predicates, and conditionals •A function is a group of statements that together perform a task. •In Lisp, functions are defined using the defun keyword. Defining Functions in LISP • The macro named defun is used for defining functions. The defun macro needs three arguments − • Name of the function • Parameters of the function • Body of the function
  • 30. Functions •The general syntax for defining a function is: (defun function-name (parameters) "Documentation string" body-of-function ) Example: (defun add (n1 n2) "Adds two numbers" (+ n1 n2) ) (write(add (10 20))
  • 31. (defun averagenum (n1 n2 n3 n4) (/ ( + n1 n2 n3 n4) 4) ) (write(averagenum 10 20 30 40))
  • 32. (defun area-circle(rad) ;Calculates area of a circle with given radius (terpri) (format t "Radius: ~5f" rad) (format t "~%Area: ~5f" (* 3.14 rad rad)) ) (area-circle 10) output: Radius: 10.0 Area: 314.1592
  • 33. Predicates Predicates are functions that test their arguments for some specific conditions and returns nil if the condition is false, or some non-nil value is the condition is true. Commonly used predicates atom It takes one argument and returns t if the argument is an atom or nil if otherwise. equal It takes two arguments and returns t if they are structurally equal or nil otherwise. evenp It takes one numeric argument and returns t if the argument is even number or nil if otherwise.
  • 34. oddp It takes one numeric argument and returns t if the argument is odd number or nil if otherwise. zerop It takes one numeric argument and returns t if the argument is zero or nil if otherwise. listp It takes one argument and returns t if the argument evaluates to a list otherwise it returns nil. greaterp It takes one or more argument and returns t if either there is a single argument or the arguments are successively larger from left to right, or nil if otherwise. lessp It takes one or more argument and returns t if either there is a single argument or the arguments are successively smaller from left to right, or nil if otherwise.
  • 35. (write (atom 'abcd)) (terpri) (write (equal 'a 'b)) (terpri) (write (evenp 10)) (terpri) (write (evenp 7 )) (terpri) (write (oddp 7 )) (terpri) (write (zerop 0.0000000001)) (terpri) (write (eq 3 3.0 )) (terpri) (write (equal 3 3.0 )) (terpri) (write (equal 3 3 )) (terpri) (write (null nil )) T NIL TNIL T NIL NIL NIL T T
  • 36. (defun factorial (num) (cond ((zerop num) 1) (t ( * num (factorial (- num 1)))) ) ) (setq n 6) (format t "~% Factorial ~d is: ~d" n (factorial n)) Factorial 6 is: 720
  • 37. Decision-making statements • Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. • Following is the general form of a typical decision making structure found in most of the programming languages −
  • 39. cond (cond (test1 action1) (test2 action2) ... (testn actionn)) (setq a 10) (cond ((> a 20) (format t "~% a is greater than 20")) (t (format t "~% value of a is ~d " a))) (value of a is 10
  • 40. if (if (test-clause) (action1) (action2)) (setq a 10) (if (> a 20) (format t "~% a is less than 20")) (format t "~% value of a is ~d " a) (value of a is 10
  • 41. if a is less than 20 value of a is 10 (setq a 10) (if (> a 20) then (format t "~% a is less than 20")) (format t "~% value of a is ~d " a)
  • 42. if a is greater than 20 value of a is 100 (setq a 100) (if (> a 20) (format t "~% a is greater than 20") (format t "~% a is less than 20")) (format t "~% value of a is ~d " a)
  • 43. when (when (test-clause) (<action1) ) (setq a 100) (when (> a 20) (format t "~% a is greater than 20")) (format t "~% value of a is ~d " a) a is greater than 20 value of a is 100
  • 44. case (case (keyform) ((key1) (action1 action2 ...) ) ((key2) (action1 action2 ...) ) ... ((keyn) (action1 action2 ...) )) (setq day 4) (case day (1 (format t "~% Monday")) (2 (format t "~% Tuesday")) (3 (format t "~% Wednesday")) (4 (format t "~% Thursday")) (5 (format t "~% Friday")) (6 (format t "~% Saturday")) (7 (format t "~% Sunday"))) Thursday
  • 45. Example (setq a 10) (cond ((> a 20) (format t "~% a is greater than 20")) (t (format t "~% value of a is ~d " a))) OUTPUT: value of a is 10
  • 46. Example 2 The if clause can be followed by an optional then clause. (setq a 10) (if (> a 20) then (format t "~% a is less than 20")) (format t "~% value of a is ~d " a) OUTPUT: a is less than 20 value of a is 10
  • 47. Example 3 You can also create an if-then-else type statement using the if clause. (setq a 100) (if (> a 20) (format t "~% a is greater than 20") (format t "~% a is less than 20")) (format t "~% value of a is ~d " a) OUTPUT: value of a is 100
  • 48. Variable Declaration (defvar x 10) (defvar y 34.567) (defvar ch nil) (defvar n 123.78) (defvar bg 11.0e+4) (defvar r 124/2) (print (type-of x)) (print (type-of y)) (print (type-of n)) (print (type-of ch)) (print (type-of bg)) (print (type-of r))
  • 49. Global Variables Global variables have permanent values throughout the LISP system and remain in effect until a new value is specified. Global variables are generally declared using the defvar construct. (defvar x 234) (write x) there is no type declaration for variables in LISP, you directly specify a value for a symbol with the setq construct. For Example (setq x 10)
  • 50. symbol-value The symbol-value function allows you to extract the value stored at the symbol storage EXAMPLE (setq x 10) (setq y 20) (format t "x = ~2d y = ~2d ~%" x y) (setq x 100) (setq y 200) (format t "x = ~2d y = ~2d" x y) ~% allows the value to be printed in next line.
  • 51. Local Variable Local variables are defined within a given procedure. The parameters named as arguments within a function definition are also local variables. Local variables are accessible only within the respective function. Like the global variables, local variables can also be created using the setq construct. There are two other constructs - let and prog for creating local variables. The let construct has the following syntax. (let ((var1 val1) (var2 val2).. (varn valn))<s-expressions>) Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the respective variables. When let is executed, each variable is assigned the respective value and lastly the s-expression is evaluated. The value of the last expression evaluated is returned. If you don't include an initial value for a variable, it is assigned to nil.
  • 52. Example- let construct (let ((x 'a) (y 'b)(z 'c)) (format t "x = ~a y = ~a z = ~a" x y z))
  • 53. Prog construct The prog construct also has the list of local variables as its first argument, which is followed by the body of the prog, and any number of s-expressions. The prog function executes the list of s-expressions in sequence and returns nil unless it encounters a function call named return. Then the argument of the return function is evaluated and returned (prog ((x '(a b c))(y '(1 2 3))(z '(p q 10))) (format t "x = ~a y = ~a z = ~a" x y z))
  • 54. Arrays LISP allows you to define single or multiple-dimension arrays using the make- array function. An array can store any LISP object as its elements. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. The number of dimensions of an array is called its rank. In LISP, an array element is specified by a sequence of non-negative integer indices. The length of the sequence must equal the rank of the array. Indexing starts from zero.
  • 55. For example, to create an array with 10- cells, named my-array, we can write − (setf my-array (make-array '(10))) The aref function allows accessing the contents of the cells. It takes two arguments, the name of the array and the index value. For example, to access the content of the tenth cell, we write − (aref my-array 9) Create a new source code file named main.lisp and type the following code in it. Live Demo (write (setf my-array (make-array '(10)))) (terpri) (setf (aref my-array 0) 25) (setf (aref my-array 1) 23) (setf (aref my-array 2) 45) (setf (aref my-array 3) 10) (setf (aref my-array 4) 20) (setf (aref my-array 5) 17) (setf (aref my-array 6) 25) (setf (aref my-array 7) 19) (setf (aref my-array 8) 67) (setf (aref my-array 9) 30) (write my-array)
  • 57. Looping not in syllabus
  • 58.
  • 59. loop (loop (s-expressions)) (setq a 10) (loop (setq a (+ a 1)) (write a) (terpri) (when (> a 15) (return a)) ) 11 12 13 14 15 16
  • 60. loop for (loop for loop-variable in <a list> do (action) ) (loop for loop-variable from value1 to value2 do (action) ) (loop for x in '(Hello World) do (format t " ~s" x) ) HELLO WORLD
  • 61. loop for (loop for loop-variable in <a list> do (action) ) (loop for loop-variable from value1 to value2 do (action) ) (loop for a from 10 to 20 do (print a) ) 10 11 12 13 14 15 16 17 18 19 20
  • 62. do (do ((variable1 value1 updated-value1) (variable2 value2 updated-value2) (variable3 value3 updated-value3) ...) (test return-value) (s-expressions) ) (do ((x 0 (+ 2 x)) (y 20 ( - y 2))) ((= x y)(- x y)) (format t "~% x = ~d y = ~d" x y) ) x = 0 y = 20 x = 2 y = 18 x = 4 y = 16 x = 6 y = 14 x = 8 y = 12
  • 63. dotimes (dotimes (n 11) (print n) (prin1 (* n n)) ) 0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
  • 64. dotimes (dotimes (n 5) (print n) (prin1 (+ n n)) ) 0 0 1 2 2 4 3 6 4 8
  • 65. dolist (dolist (n '(1 2 3 4 5 6 7 8 9)) (format t "~% Number: ~d Square: ~d" n (* n n)) ) Number: 1 Square: 1 Number: 2 Square: 4 Number: 3 Square: 9 Number: 4 Square: 16 Number: 5 Square: 25 Number: 6 Square: 36 Number: 7 Square: 49 Number: 8 Square: 64 Number: 9 Square: 81