Basic Lisp Operations

Similar documents
Universitetet i Linköping Institutionen för datavetenskap Anders Haraldsson

How To Program In Scheme (Prolog)

Chapter 15 Functional Programming Languages

Programming Languages in Artificial Intelligence

QUERYING THE COMPONENT DATA OF A GRAPHICAL CADASTRAL DATABASE USING VISUAL LISP PROGRAM

Magit-Popup User Manual

Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I

Symbol Tables. Introduction

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Data Mining Algorithms Parallelizing in Functional Programming Language for Execution in Cluster

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CS 1133, LAB 2: FUNCTIONS AND TESTING

Java Interview Questions and Answers

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Compiler Construction

C++ INTERVIEW QUESTIONS

Moving from CS 61A Scheme to CS 61B Java

Business Application

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

13 Classes & Objects with Constructors/Destructors

Writing Functions in Scheme. Writing Functions in Scheme. Checking My Answer: Empty List. Checking My Answer: Empty List

Party Time! Computer Science I. Signature Examples PAIR OBJ. The Signature of a Procedure. Building a Set. Testing for Membership in a Set

R Language Definition

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Robot Programming with Lisp

How To Understand And Understand Common Lisp

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Habanero Extreme Scale Software Research Project

While Loops and Animations

Programming Languages CIS 443

Bash shell programming Part II Control statements

Parameter passing in LISP

Object-Oriented Programming Lecture 2: Classes and Objects

Base Conversion written by Cathy Saxton

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

1 Idioms, Patterns, and Programming

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Curriculum Map. Discipline: Computer Science Course: C++

Pemrograman Dasar. Basic Elements Of Java

ios Dev Crib Sheet In the Shadow of C

The C Programming Language course syllabus associate level

The different kinds of variables in a Java program

1 Abstract Data Types Information Hiding

Why? A central concept in Computer Science. Algorithms are ubiquitous.

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

[Refer Slide Time: 05:10]

Intelligent Agents. Chapter 2. Chapter 2 1

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Java Programming Fundamentals

PROBLEMS (Cap. 4 - Istruzioni macchina)

Unix Scripts and Job Scheduling

High-Level Language. Building a Modern Computer From First Principles.

Chapter 5. Selection 5-1

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

The Cool Reference Manual

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Pico Lisp A Radical Approach to Application Development

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

J a v a Quiz (Unit 3, Test 0 Practice)

5. Advanced Object-Oriented Programming Language-Oriented Programming

Dialog planning in VoiceXML

Functional Programming

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Normal Form vs. Non-First Normal Form

Chapter 3: Restricted Structures Page 1

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Sample Questions Chapter 2. Stoker

The Needle Programming Language

C Compiler Targeting the Java Virtual Machine

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Semantic Analysis: Types and Type Checking

SIMULATION COMPARISON AND STEPS TO DO SIMULATION BY AUTOLISP (AUTOCAD) AND PRO-E FOR GEAR PAIR.

Chapter 7: Functional Programming Languages

2. Abstract State Machines

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Roman Numerals Case Study 1996 M. J. Clancy and M. C. Linn

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

OMPT and OMPD: OpenMP Tools Application Programming Interfaces for Performance Analysis and Debugging

by Mario Fusco Monadic

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Transcription:

Basic Lisp Operations BLO-1

Function invocation It is an S-expression just another list! ( function arg1 arg2... argn) First list item is the function prefix notation The other list items are the arguments to the function. Arguments can themselves be lists» (+ 1 2 3 (+ 4 5 6) 7 8 9 ) ==> 45» Outer + has 7 arguments, inner + has 3 arguments» Arguments are evaluated before the function BLO-2

Basic Functions Can build Lisp out of these functions List access & creation» car or first access first in list» cdr or rest access all but first» cons construct a list cell Other» quote or ' take literally, do not interpret» atom true if argument is an atom» eq true if arguments are same object» cond conditional generalized if... then... else... BLO-3

List access functions Original name Pronounced as could-er List Cell car / first cdr / rest Modern name ( car '( a b c ) ) ( first '( a b c ) ) a ( cdr '( a b c ) ) ( rest '( a b c ) ) ( b c ) BLO-4

CAR and CDR Structural View 1 ( A B C D ) CAR = A Copies pointer A Copies pointer CDR = ( B C D ) B C D nil BLO-5

CAR and CDR Structural View 2 ( A. B ) CAR = A CDR = B Copy pointer A B Copy pointer BLO-6

( car '( a b c ) ) why the quote? Recall that arguments are evaluated before the function If we wrote ( car ( a b c) )» argument ( a b c ) would be evaluated before the car» a would be a function call» but we literally want the list (a b c) not the result of evaluating a on the arguments b and c. '(...) is syntactic sugar for (quote... ) where Lisp treats the function quote as a special function whose arguments are not evaluated first ( car '( a b c ) ) ( car ( quote ( a b c ) ) ) BLO-7

Why the names CAR and CDR? Original Lisp developed for an IBM 704 computer which had 18 bit registers Pairs of registers could be handled as a single 36 bit ʻwordʼ one word = one lisp cell address register decrement register CAR Contents Address Register CDR Contents Decrement Register BLO-8

Short hand for nested car's and cdr's Accessing deeper into Lisp structures occurs so frequently that additonal functions are introduced into Lisp. For example ( cdddar... ) (cdr ( cdr ( cdr (car... )))) Interpret from right to left Length depends upon the implementation. BLO-9

Creating a New Lisp Cell cons Only one constructor function cons Copies pointers to the arguments A Copy pointer Laws» (car (cons A B)) = A» (cdr (cons A B)) = B ( cons A B ) Copy pointer B BLO-10

Destructive List Construction Cons is expensive as it creates a new cell» memory allocation is invoked > But it is non destructive no side effects Following is dangerous do not use in the course! For efficiency Common Lisp provides a set of destructive operations they change lists» ( replca cell newvalue ) & ( replcd cell newvalue ) > Replace the car and cdr fields of cell with ponters to newvalue» ( nconc x y ) > Replace the cdr field of the last component of x with a pointer to y BLO-11

SETQ Define a symbol value (setq x value)» If the symbol x does not exist it is created» Symbol x is given the value value In this course USE ONLY AT THE GLOBAL LEVEL to create symbols required to test your programs Example» (setq x ʻ(1+ 4)) sets the value of x to the list (1+ 4)» Note the x is not quoted but the second argument is if you do not want to evaluate it. BLO-12

( setq x ʻy )» x has the value y Compare SET and SETQ ( set x ʻz )» x still has the value y» but a new symbol y is created with the value z» why? See the notes on symbols BLO-13

DEFUN define a function ( defun functionname ( argumentlist ) ) List of S-expressions to evaluate when the function is invoked usually only one S-expression Example ( defun add ( a b ) (+ a b ) ) Value of the function is the value of the last S-expression that is executed Functions in Lisp are typically small» rarely more than 1/2 a page in length BLO-14

eq (eq x y)» Shallow equality» True if x and y are the same internal Lisp object > It is possible, in some implementations, that a number or character may be represented by different objects As a consequence, (eq 3 3) may be either true or false BLO-15

eql (eql x y)» Shallow equality» Either eq or the same number or character > Note that 3.0 and 3 are different numbers. > Implementation may have eql either true or false BLO-16

equal (equal x y)» Deep equality» True if x and y have equivalent values eq is stricter than eql, which is stricter than equal BLO-17