Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
1 Programming Languages and Paradigms Lisp Programming.
TUTORIAL 3 CSCI3230 ( First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
TES3111 October 2001 Artificial Intelligence LISP.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
Common lisp A functional programming language. Useful URL:
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
PRACTICAL COMMON LISP Peter Seibel 1.
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
EZGİ GENÇ History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
LISP A brief overview.
Using Lisp Lisp is a interactive system
CS 36 – Chapter 11 Functional programming Features Practice
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Abstraction and Repetition
Functional Programming: Lisp
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
LISP: Basic Functionality
Lisp.
Allegro CL Certification Program
Lisp: Representation of Data
The general format of case is the following: (case <key form>
Presentation transcript:

Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression; print the result of evaluation end loop.

Sample Session  (* 2 (cos 0) (+ 4 6))  20.0  (defun double (x) (* x 2))  DOUBLE  (double 3)  6

This is a program typed into a text file ;; Triple the value of a number ;; (defun triple (X) "Compute three times X." ; Inline comments can (* 3 X)) ; be placed here. ;; ;; Negate the sign of a number ;; (defun negate (X) "Negate the value of X." ; This is a comment (- X))

Name the file testing.lisp. Now load the definition into the LISP environment by typing: > (load "testing.lisp") T

Recursions and Conditionals We can implement a function to compute factorials using recursion: (defun factorial (N) ;; Compute the factorial of N. (if (= N 1) 1 (* N (factorial (- N 1))))) Note: LISP treats NIL as false, and everything else as true.

Relational Operators

The Trace Function  (trace factorial) (FACTORIAL) USER(12): (factorial 4) 0: (FACTORIAL 4) 1: (FACTORIAL 3) 2: (FACTORIAL 2) 3: (FACTORIAL 1) 3: returned 1 2: returned 2 1: returned 6 0: returned 24  24

Recall the definition of Fibonacci numbers: Fib(n) = 1 for n = 0 or n = 1 Fib(n) = Fib(n-1) + Fib(n-2) for n > 1 LISP code: (defun fibonacci (N) ;; Compute the N'th Fibonacci number (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))

Built-in Shorthands and Predicates

The or form logical operator It evaluates its arguments from left to right, returning non-NIL immediately if it encounters an argument that evaluates to non- NIL. It evaluates to NIL if all tests fail.

Logical Operators

Lists  (cons 1 (cons 2 nil))  (1 2)  (quote ( ))  ( )  '( )  ( )

Your session may look more like this: USER(24): (first '(2 4 8)) 2 USER(25): (rest '(2 4 8)) (4 8) USER(26): (first (rest '(2 4 8))) 4 USER(27): (rest (rest '(2 4 8))) (8) USER(28): (rest (rest (rest '(8)))) NIL

Recognizers Corresponding to each constructor of a data type is a recognizer. In the case of list, they are null for nil and consp for cons. Given a list L, (null L) returns t iff L is nil and (consp L) returns t iff L is constructed from cons.

USER(29): (null nil) T USER(30): (null '(1 2 3)) NIL USER(31): (consp nil) NIL USER(32): (consp '(1 2 3)) T

recursive functions that traverse a list. The LISP built-in function list-length counts the number of elements in a list. USER(33): (list-length '( )) 8

we could implement our own version of list- length as follows: (defun recursive-list-length (L) (if (null L) 0 (1+ (recursive-list-length (rest L)))))

Symbols USER(45): 'a ; LISP is case-insensitive. A USER(46): 'A ; 'a and 'A evaluate to the same symbol. A USER(47): 'apple2 ; Both alphanumeric characters... APPLE2 USER(48): 'an-apple ;... and symbolic characters are allowed. AN-APPLE USER(49): t ; Our familiar t is also a symbol. T USER(50): 't ; In addition, quoting is redundant for t. T USER(51): nil ; Our familiar nil is also a symbol. NIL USER(52): 'nil ; Again, it is self-evaluating. NIL

With symbols, we can build more interesting lists: USER(53): '(how are you today ?) ; A list of symbols. (HOW ARE YOU TODAY ?) USER(54): '(1 + 2 * x) ; A list of symbols and numbers. (1 + 2 * X) USER(55): '(pair (2 3)) ; A list containing 'pair and '(2 3). (pair (2 3)) Notice that the list (pair (2 3)) has length 2:

Example: nth LISP defines a function (nth N L) that returns the N'th member of list L (assuming that the elements are numbered from zero onwards): USER(59): (nth 0 '(a b c d)) A USER(60): (nth 2 '(a b c d)) C