Department of Computer and information Science Norwegian University of Science and Technology

Similar documents
FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

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

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

INF4820. Common Lisp: Closures and Macros

Recursion & Iteration

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

A little bit of Lisp

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Common LISP Tutorial 1 (Basic)

Lecture Notes on Lisp A Brief Introduction

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

Lisp Basic Example Test Questions

Functional programming with Common Lisp

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

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

Artificial Intelligence Programming

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

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

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Common Lisp. Blake McBride

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

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

Allegro CL Certification Program

Common LISP-Introduction

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

Imperative, OO and Functional Languages A C program is

Documentation for LISP in BASIC

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

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

Robot Programming with Lisp

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lecture #2 Kenneth W. Flynn RPI CS

Functional Programming. Pure Functional Programming

Allegro CL Certification Program

Lisp. Versions of LISP

A Brief Introduction to Common Lisp

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Introduction to Lisp

Functional Programming with Common Lisp

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Functional Programming. Pure Functional Languages

Review of Functional Programming

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

Midterm Examination (Sample Solutions), Cmput 325

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

Symbolic Computation and Common Lisp

Functional Programming. Big Picture. Design of Programming Languages

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

Intro. Scheme Basics. scm> 5 5. scm>

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

Heap storage. Dynamic allocation and stacks are generally incompatible.

Lambda Calculus. Gunnar Gotshalks LC-1

Lecture #24: Programming Languages and Programs

AllegroCache: an Introduction

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

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

CS 314 Principles of Programming Languages

SCHEME AND CALCULATOR 5b

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CSC 533: Programming Languages. Spring 2015

19 Machine Learning in Lisp

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

CS 842 Ben Cassell University of Waterloo

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Announcements. Today s Menu

Sample Final Exam Questions

Functions, Conditionals & Predicates

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

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

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

SOFTWARE ARCHITECTURE 6. LISP

Introduction to Functional Programming and basic Lisp

CS A331 Programming Language Concepts

Functional Programming

Streams and Lazy Evaluation in Lisp

15 Unification and Embedded Languages in Lisp

UMBC CMSC 331 Final Exam

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Chapter 1. Fundamentals of Higher Order Programming

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Functional Programming Languages (FPL)

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Introduction to lambda calculus Part 3

Lambda Calculus LC-1

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

University of Maine School of Computing and Information Science COS 470/570

CS 275 Name Final Exam Solutions December 16, 2016

Functional Languages. Hwansoo Han

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

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

CS 61A Discussion 8: Scheme. March 23, 2017

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Transcription:

Department of Computer and information Science Norwegian University of Science and Technology http://www.idi.ntnu.no/ A Crash Course in LISP MNFIT272 2002 Anders Kofod-Petersen anderpe@idi.ntnu.no

Introduction What are the topics for this crash course? Overview Symbol manipulation LISP Data Functions Recursion Assignment Functional programming Iteration Lists

Overview LISP takes its name from List Proccessing, some argue that it comes from Lots of Irritating Silly Parentheses LISP has the easiest syntax of all languages LISP is oriented towards the manipulation of symbols LISP is very flexible in that users can change the syntax of the language if they don t like it

New tools You can do things in LISP that can t be done in ordinary programming languages To write a function that returns the sum: (defun sum (n) (let ((s 0)) (dotimes (i n s) (incf s i)))) int sum(int n){ int i,s=0; for(1=0;i<n;i++){ s+=i;} return(s); } To write a function that takes a number n and returns a function that adds n n to its arguments: (defun addn (n) Sorry, no can do! # (lambda (x) (+ x n)))

Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental things formed from bits in LISP are atoms Examples are: 3.1416, This-is-an-atom, nil Groups of atoms can form sentence like objects called lists Examples are: (1 2 3) (cow horse (hog pig)) Atoms and lists are collectively called symbolic expressions Programs and data are represented the same way is (+ 10 15) a program or data?

LISP Data LISP offers all the data types that other languages do New data types are: Symbols LISP Lists (LISP has 2 new types) You can build lists by list: (list LISP has (+ 1 1) new types ) Lists are build by using cons: (cons a (b c d)) -- (a b c d) Lists are manipulated by using car and cdr: (car (a b c)) -- a (cdr (a b c)) -- (c d)

Functions You can define new functions with defun Defun takes three or more arguments: 1. A name 2. A list of parameters 3. One or more expressions that makes the body of the function (defun square (x) (* x x)) Notice that LISP uses prefix and not infix notation LISP makes no distinction between a program, a procedure, or a function Functions do for everything You can also read and write input and output (defun ask (string) (format t A string) (read))

Recursion Recursion is a big thing in LISP When writing recursive functions two issues are paramount: 1. Handle the trivial case first 2. Then handle the recursive case Defining a function that calculates the length of a list: (defun len (lst) (if (null lst) 0 (+ (len (cdr lst)) 1))) > (len (a b c d)) 4 Recursive functions can be used for more complex problems: (defun fibonacci (x) (if (<= x 2) 1 (+ (fibonacci (- x 2))(fibonacci (1- x))))) > (fibonacci 10) 55

Variables There are several ways of defining variables in LISP: 1. Using the setf construction 2. Using the let construction (defun showvar (x y) (setf var x) (let ((var y)) (print var)) (print var) t) > (showvar 10 5) 5 10 t For global parameters use defparameter or defconstant (defparameter *global* 100)

Functional programming Functional programming means writing functions that returns values > (setf lst (H e l l o)) > (remove l lst) (H e o) > lst (H e l l o) The most important advantage with this non-destructive way of writing, is the possibility if testing each function as you write them, and guarantee that they are correct If you want to write destructive code you can do: > (setf lst (remove l lst))

Iteration It can sometimes be more natural repeat a procedure in an interactive way, as compared to a recursive way Redefining the length function: (defun lenit (lst) (let ((len 0)) (dolist (obj lst) (setf len (+ len 1))) len)) > (lenit (a b c d)) 4 dolist is not the only interactive function, others are: do, dotimes, and loop For handling batch like coding, LISP offers constructions like progn (defun write2 (lst) (progn (print lst) (print (reverse lst))) > (write2 (a b c d e)) (a b c b a) (e d c b a)

LISP at IDI The LISP used at IDI is Allegro Common LISP; a de facto standard To get LISP to work in emacs, put the following into your.emacs file: (setq inferior-lisp-program "/local/acl/mlisp") (load "/usr/local/acl/eli/fi-site-init.el") Notice that emacs is controlled with LISP! 1. Open an emacs 2. Type <esc>-x fi:commmon-lisp 3. Make a new frame 4. type <esc>-x lisp-mode 5. You can now write your lisp code in one window, and compile it with <ctrl>-x

Litterature LISP 3rd edition Patrick Henry Winston Berthold Klaus Paul Horn ISBN: 0-201-08379-1 ANSI Common Lisp Paul Graham ISBN: 0-13-370875-6 Common Lisp the Language, 2nd edition Guy L. Steel ISBN: 1-55558-041-6 http://www.math.uio.no/cltl/cltl2.html