Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Programming Languages and Paradigms Lisp Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction to C Programming
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
String Escape Sequences
Basic Elements of C++ Chapter 2.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Objectives You should be able to describe: Data Types
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Common lisp A functional programming language. Useful URL:
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Chapter 3 Arithmatic Operations 3.1 Operators An operator is a symbol or world that represents some operation that is performed on one or more data values.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Visual Basic 6 (VB6) Data Types, And Operators
Object Oriented Programming
Primitive Data, Variables, Loops (Maybe)
IDENTIFIERS CSC 111.
Arithmetic operations, decisions and looping
Chapter 2 - Introduction to C Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
OPERATORS AND EXPRESSIONS IN C++
DATA TYPES There are four basic data types associated with variables:
DATA TYPES AND OPERATIONS
Presentation transcript:

Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402

Today Discussion: LISP Programs Artificial Intelligence CS-402

Example Create new source code file named main.lisp and type the following code in it. (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) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is. x = 10 y = 20 x = 100 y = 200

LISP - Constants In LISP, constants are variables that never change their values during program execution. Constants are declared using the defconstant construct. Example The following example shows declaring a global constant PI and later using this value inside a function named area-circle that calculates the area of a circle. The defun construct is used for defining a function. Create a new source code file named main.lisp and type the following code in it. (defconstant PI ) (defun area-circle(rad) (terpri) (format t "Radius: ~5f" rad) (format t "~%Area: ~10f" (* PI rad rad))) (area-circle 10)

Example Output When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is. Radius: 10.0 Area:

LISP - Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. LISP allows numerous operations on data, supported by various functions, macros and other constructs. The operations allowed on data could be categorized as:  Arithmetic Operations  Comparison Operations  Logical Operations  Bitwise Operations

Arithmetic Operations The following table shows all the arithmetic operators supported by LISP. Assume variable A holds 10 and variable B holds 20 then : OperatorDescriptionExample +Adds two operands(+AB) will give 30 -Subtracts second operand from the first (- A B) will give -10 *Multiplies both operands (* A B) will give 200 /Divides numerator by de-numerator (/ B A) will give 2 mod,remModulus Operator and remainder of after an integer division (mod B A )will give 0 incfIncrements operator increases integer value by the second argument specified (incf A 3) will give 13 decfDecrements operator decreases integer value by the second argument specified (decf A 4) will give 9 OperatorDescriptionExample +Adds two operands(+AB) will give 30 -Subtracts second operand from the first (- A B) will give -10 *Multiplies both operands (* A B) will give 200 /Divides numerator by de-numerator (/ B A) will give 2

Continued …… mod,remModulus Operator and remainder of after an integer division (mod B A )will give 0 incfIncrements operator increases integer value by the second argument specified (incf A 3) will give 13 decfDecrements operator decreases integer value by the second argument specified (decf A 4) will give 9

Comparison Operations Following table shows all the relational operators supported by LISP that compares between numbers. However unlike relational operators in other languages, LISP comparison operators may take more than two operands and they work on numbers only. Assume variable A holds 10 and variable B holds 20, then : OperatorDescriptionExample =Checks if the values of the operands are all equal or not, if yes then condition becomes true. (= A B) is not true. /=Checks if the values of the operands are all different or not, if values are not equal then condition becomes true. (/= A B) is true.

Continued…….. >Checks if the values of the operands are monotonically decreasing. (> A B) is not true. <Checks if the values of the operands are monotonically increasing. (< A B) is true. >=Checks if the value of any left operand is greater than or equal to the value of next right operand, if yes then condition becomes true. (>= A B) is not true. <=Checks if the value of any left operand is less than or equal to the value of its right operand, if yes then condition becomes true. (<= A B) is true. maxIt compares two or more arguments and returns the maximum value. (max A B) returns 20 minIt compares two or more arguments and returns the minimum value. (min A B) returns 20

Logical Operations on Boolean Values OperatorDescriptionExample AndIt takes any number of arguments. The arguments are evaluated left to right. If all arguments evaluate to non-nil, then the value of the last argument is returned. Otherwise nil is returned. (and A B) will return NIL. OrIt takes any number of arguments. The arguments are evaluated left to right until one evaluates to non- nil, in such case the argument value is returned, otherwise it returns nil. (or A B) will return 5. notIt takes one argument and returns t if the argument evaluates to nil. (not A) will return T.

LISP - Functions A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task. 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 Syntax for defun is: (defun name (parameter-list) "Optional documentation string." body)

Example 1 Let's write a function named averagenum that will print the average of four numbers. We will send these numbers as parameters. Create a new source code file named main.lisp and type the following code in it. (defun averagenum (n1 n2 n3 n4) (/ ( + n1 n2 n3 n4) 4) ) (write(averagenum ))When you execute the code, it returns the following result: 25

Example 2 Let's define and call a function that would calculate the area of a circle when the radius of the circle is given as an argument. (defun area-circle(rad) "Calculates area of a circle with given radius" (terpri) (format t "Radius: ~5f" rad) (format t "~%Area: ~10f" (* rad rad)) ) (area-circle 10) When you execute the code, it returns the following result: Area:

LISP - 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. The following table shows some of the most commonly used predicates :

LISP - Predicates PredicateDescription atomIt takes one argument and returns t if the argument is an atom or nil if otherwise. equalIt takes two arguments and returns t if they are structurally equal or nil otherwise eqIt takes two arguments and returns t if they are same identical objects, sharing the same memory location or nil otherwise eqlIt takes two arguments and returns t if the arguments are eq, or if they are numbers of the same type with the same value, or if they are character objects that represent the same character, or nil otherwise

Example Create a new source code file named main.lisp and type the following code in it. (defun factorial (num) (cond ((zerop num) 1) (t ( * num (factorial (- num 1)))) ) (setq n 6) (format t "~% Factorial ~d is: ~d" n (factorial n)) When you execute the code, it returns the following result: Factorial 6 is: 720

LISP - Numbers Common Lisp defines several kinds of numbers. The number data type includes various kinds of numbers supported by LISP. The number types supported by LISP are:  Integers  Ratios  Floating-point numbers  Complex numbers

Various Numeric Types in LISP Data typeDescription fixnumThis data type represents integers which are not too large and mostly in the range -215 to (it is machine-dependent) bignumThese are very large numbers with size limited by the amount of memory allocated for LISP, they are not fixnum numbers. ratioRepresents the ratio of two numbers in the numerator/denominator form. The / function always produce the result in ratios, when its arguments are integers. floatIt represents non-integer numbers. There are four float data types with increasing precision. complexIt represents complex numbers, which are denoted by #c. The real and imaginary parts could be both either rational or floating point numbers.

Example Create a new source code file named main.lisp and type the following code in it. (write (/ 1 2)) (terpri) (write ( + (/ 1 2) (/ 3 4))) (terpri) (write ( + #c( 1 2) #c( 3 -4))) When you execute the code, it returns the following result: ½ 5/4 #C(4 -2)

Number Functions The following table describes some commonly used numeric functions: FunctionDescription +, -, *, /Respective arithmetic operations sin, cos, tan, acos, asin, atanRespective trigonometric functions. sinh, cosh, tanh, acosh, asinh, atanhRespective hyperbolic functions. expExponentiation function. Calculates e x

Continued ………… exptExponentiation function, takes base and power both. sqrtIt calculates the square root of a number. logLogarithmic function. It one parameter is given, then it calculates its natural logarithm, otherwise the second parameter is used as base. conjugateIt calculates the complex conjugate of a number. In case of a real number, it returns the number itself. absIt returns the absolute value (or magnitude) of a number. gcdIt calculates the greatest common divisor of the given numbers lcmIt calculates the least common multiple of the given numbers isqrtIt gives the greatest integer less than or equal to the exact square root of a given natural number.

Continued …… mod, remReturns the remainder in a division operation. floatConverts a real number to a floating point number. rational, rationalizeConverts a real number to rational number. numerator, denominatorReturns the respective parts of a rational number. realpart, imagpartReturns the real and imaginary part of a complex number.

Example Conversions: Example Program of Celsius to Fahrenheit (defun convert () (format t “Enter Fahrenheit “) (LET (fahr) (SETQ fahr (read fahr)) (APPEND ‘(celsisus is) (*(- fahr 32)(/ 5 9)) ) )

Best of Luck