Introduction to Functional Programming and basic Lisp

Similar documents
Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

SOFTWARE ARCHITECTURE 6. LISP

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

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

Lisp and Prolog Background

Lisp History. Lisp and Prolog Background. Lisp Use. Lisp Availability

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

CS 314 Principles of Programming Languages

by the evening of Tuesday, Feb 6

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

15 Unification and Embedded Languages in Lisp

Functional Programming. Big Picture. Design of Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

Lecture Notes on Lisp A Brief Introduction

Heap storage. Dynamic allocation and stacks are generally incompatible.

A little bit of Lisp

FP Foundations, Scheme

Recursive Functions of Symbolic Expressions and Their Application, Part I

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Lisp. Versions of LISP

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Functional Programming

1.3. Conditional expressions To express case distinctions like

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Lisp Basic Example Test Questions

Functional Programming. Pure Functional Languages

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Functional Programming. Pure Functional Languages

Lecture #2 Kenneth W. Flynn RPI CS

A Small Interpreted Language

Imperative, OO and Functional Languages A C program is

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

John McCarthy IBM 704

Principles of Programming Languages 2017W, Functional Programming

Functional Programming Languages (FPL)

Functional programming with Common Lisp

Review of Functional Programming

Functional Programming. Pure Functional Programming

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Control in Sequential Languages

Common LISP-Introduction

ALISP interpreter in Awk

Lambda Calculus see notes on Lambda Calculus

a little more on macros sort of like functions, but..

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Common LISP Tutorial 1 (Basic)

Introduction to Scheme

Associative Database Managment WIlensky Chapter 22

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Associative Database Managment

CS A331 Programming Language Concepts

Section 10: LISP to Scheme. Evolution of Software Languages

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Functional programming in LISP

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CSc 520 Principles of Programming Languages

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

A Quick Introduction to Common Lisp

CSCI337 Organisation of Programming Languages LISP

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Emacs Lisp. Introduction A little history. CSc 372, Fall 2006 Emacs Lisp, Slide 1 W. H. Mitchell

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

comparing Lisp and Prolog

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Lambda Calculus LC-1

User-defined Functions. Conditional Expressions in Scheme

Imperative languages

CS 314 Principles of Programming Languages

Functions, Conditionals & Predicates

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

LECTURE 16. Functional Programming

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

CS61A Midterm 2 Review (v1.1)

CSc 520 Principles of Programming Languages

Announcements. Today s Menu

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

Emacs Lisp Introduction

Functional Programming Lecture 1: Introduction

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Recursion & Iteration

Programming Languages

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

A Brief Introduction to Scheme (II)

Lambda Calculus. Gunnar Gotshalks LC-1

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

An Explicit-Continuation Metacircular Evaluator

Introduction to lambda calculus Part 3

Introduction 2 Lisp Part I

Transcription:

Introduction to Functional Programming and basic Lisp Based on Slides by Yves Lespérance & Peter Roosen-Runge 1 Functional vs Declarative Programming declarative programming uses logical statements to describe objects. Prolog is an example of this kind of language. functional programming uses mathematical functions and functional expressions to describe objects. Functional programming has its roots in lambda calculus Lisp is a functional language. 2

Functional Programming Functional programming is not based on assignments that change the state. Functions specify other values in terms of existing data without changing it. This allows all sorts of clever implementations e.g. on parallel hardware. 3 Background on LISP Acronym for LISt Processing created by the AI pioneer John McCarthy widely used in research on AI for over 40 years. Used in industry to develop expert systems & other AI applications found inside applications like Emacs and AutoCAD as an embedded language makes the embedding application easily extensible 4

Lisp as an extensional language embedding Lisp makes it easy to extend an application creation of new languages built "on" Lisp industrial-strength versions are usually standardized on Common Lisp 5 LISP Interpreter An interactive environment, always evaluating input To run LISP at prism labs, type: "clisp To exit: Ctrl+D To load the file lp.lsp, in C:\MyFolder, use the following command: (load C://MyFolder/lp.lsp ) 6

S-expressions A symbolic expression (s-expression) is defined inductively as an atom (number or word), or dotted pair of s-expressions (e.g. (x.y) where x and y are s-expressions) lists are the main kind of s-expressions Example: (a b c) or a. (b. (c. nil)) - nil is the same as () - the only atom that is a list 7 disassembly functions which extract the two parts of a dotted pair: first extracts the first part, also called car rest extracts the second part, also called cdr 8

contents of a dotted-pair first rest atom or dotted pair atom or dotted pair 9 new names for old car = function which returns the content referenced by address-register cdr = function which returns content referenced by decrement register many books cling to car and cdr for 'backwards compatibility'. can use first and rest you can use either - the Lisp interpreter doesn t care. 10

list structure first rest atom or dotted pair nil or list 11 functional expressions terms (functional expressions) are represented as lists write f(x, y) as (f x y). (a b c) represents the term a(b, c). already we see a bit of the power of symbolic computing: expressions have same form as data a function name is just an atom (a symbol) could itself be computed 12

evaluating functions evaluating (function arg1 arg2.. ) applies the system function eval to each argument then applies the function to the results (+ 2 (+ 3 5)) eval(2) = 2 eval((+ 3 5)) = +(eval(3), eval(5)) = +(3,5) = 8 + (2, 8) = 10 13 blocking evaluation try evaluating (reverse (a b)) --> error no function a defined (reverse (+ 1 2)) --> error 3 is not a list how to block evaluation of an s- expression? quote it 14

the uses of quotation try evaluating (reverse (quote (a b))) --> (B A) quote returns its argument unevaluated (reverse '(+ 1 2)) --> (2 1 +) '<s-expression> = (quote <s-expression>) (equal '(reverse (a b)) '(b a)))? NIL -- why? (equal (reverse '(reverse (a b))) '((a b) reverse))) T 15 List Processing Functions > (car (a b c)) --> A > (cdr (a b c)) --> (B C) > (car(cdr(car ((a b))))) --> (B) Other built-in functions for list processing include: cons, append, list, > (cons a (b c)) --> (A B C) > (append (a) (b) (c)) --> (A B C) > (list a b c) --> (A B C) 16

oceans of functions the basis for Lisp programs is the concept of a function and variants with special properties Common Lisp has several hundred built-in functions many are redundant - could be replaced by expressions involving other functions 17 Function definitions Written as (defun function-name arg-list result-spec). function-name is a symbol. arg-list is a list of symbols, the parameters. result-spec is an expression whose value is the result of the function. When a function application is evaluated, substitute actual arguments for parameters in result-spec and return its value. 18

Function Example 1 > (defun avg (x y) (/ (+ x y) 2.0)) AVG > (avg 1 2) 1.5 19 Function Example 2 Rewrite (John P Doe) as (Doe John P) (defun last_name_first (name_list) (cons (third name_list) (cons (first name_list) (cons (second name_list) nil)))) 20

Functions in LISP functions are considered as first class objects in Lisp. A function can take another function as an argument and a function can return a function as a value. This is what make functional programming very powerful. In a sense you can define your own control structures and manipulate programs! 21 More. Practical Common Lisp Book online http://www.gigamonkeys.com/book/ Notes on Lambda Calculus http://www.mathstat.dal.ca/~selinger/papers/la mbdanotes.pdf 22