A Brief Introduction to Common Lisp

Similar documents
INF4820. Common Lisp: Closures and Macros

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

Common LISP Tutorial 1 (Basic)

A Genetic Algorithm Implementation

A little bit of Lisp

Object Oriented Programming (OOP)

Robot Programming with Lisp

Documentation for LISP in BASIC

Functional Programming

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming with Common Lisp

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

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

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

Common LISP-Introduction

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

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

Common Lisp Object System Specification. 1. Programmer Interface Concepts

Functional Programming. Pure Functional Programming

Custom Specializers in Object-Oriented Lisp

Symbolic Computation and Common Lisp

Using a waiting protocol to separate concerns in the mutual exclusion problem

arxiv: v1 [cs.pl] 3 Dec 2014

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

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

Allegro CL Certification Program

Functional Programming Lecture 1: Introduction

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

Concepts of programming languages

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Programming Language Pragmatics

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Functional programming techniques

Section 10: LISP to Scheme. Evolution of Software Languages

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

Programming Languages at a Glance

Robot Programming with Lisp

LFE - a lisp on the Erlang VM

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

MIDTERM EXAMINATION - CS130 - Spring 2005

4. Functional Programming Language-Oriented Programming

Streams and Lazy Evaluation in Lisp

Associative Database Managment WIlensky Chapter 22

Concepts of Programming Languages

Chapter 1. Fundamentals of Higher Order Programming

Object oriented programming

Control in Sequential Languages

Associative Database Managment

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

Artificial Intelligence Lecture 1

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

CSE341, Spring 2013, Final Examination June 13, 2013

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

CS 415 Midterm Exam Spring 2002

1 CLWEB INTRODUCTION 1

2. Reasons for implementing clos-unit

CS 360 Programming Languages Interpreters

CSCI337 Organisation of Programming Languages LISP

UMBC CMSC 331 Final Exam

Seminar in Programming Languages

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

Programming Systems in Artificial Intelligence Functional Programming

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

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

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

Functions and Recursion. Dr. Philip Cannata 1

CSE341 Autumn 2017, Final Examination December 12, 2017

COMMOM OBJECT ORIENTED LISP SYSTEMS

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

Functional Programming Languages (FPL)

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Common Lisp. Blake McBride

Chapter 11 :: Functional Languages

Imperative languages

An Explicit-Continuation Metacircular Evaluator

Artificial Intelligence Programming

Lisp. Versions of LISP

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

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

Announcements. Today s Menu

Introduction 2 Lisp Part III

CS 415 Midterm Exam Spring SOLUTION

SOFTWARE ARCHITECTURE 6. LISP

Stop coding Pascal. Saturday, April 6, 13

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

Extra Lecture #7: Defining Syntax

FP Foundations, Scheme

Functional Programming

Lexical vs. Dynamic Scope

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

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

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

CS 314 Principles of Programming Languages

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

AllegroCache: an Introduction

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Transcription:

A Brief Introduction to Common Lisp David Gu Schloer Consulting Group david_guru@gty.org.in

A Brief History Originally specified in 1958, Lisp is the second-oldest highlevel programming language in widespread use today; only Fortran is older (by one year). Lisp stands for LISt Processing (while Fortran stands for FORmula TRANslator). 1958 ~ 1980: Various dialects and systems, MacLisp, InterLisp, and Lisp Machines.

A Brief History 1975 ~ 1980: Scheme, the first dialect choosing lexical scope, developed in MIT. 1980 ~ 1990: Common Lisp, an industry-level language, published in ANSI standard. 1990 ~ Now: Various implementation for Common Lisp and Scheme; More 3rd party libraries; A new successful dialect Clojure.

First Impression // C Code int factorial(int n){ if(n==0) return 1; else return n * factorial(n-1); } ;; Common Lisp Code (defun factorial (n) (if (= n 0) 1 (* n (factorial (- n 1)))))

First Impression: Evolution // C code, using tail recursion int factorial_helper(int result, int count){ if(count == 0) return result; else return factorial_helper(result*count, count-1); } int factorial(int n){ return factorial_helper(1, n); }

First Impression: Evolution ;; Common Lisp code, using tail recursion (defun factorial (n) (declare (optimize (speed 3))) (labels ((iter (result count) (if (= count 0) result (iter (* result count) (1- count))))) (iter 1 n)))

First Impression: Final 'Product' ;;;; can even use a function called disassemble ;;;; to check the assemble code. (defun factorial (n) (declare (optimize (speed 3))) (labels ((iter (result count) (declare (type fixnum result count)) (if (= count 0) result (iter (the fixnum (* result count)) (the fixnum (- count 1)))))) (iter 1 n)))

Multi-Paradigms: Functional Lambda(λ) ((lambda (x y) (+ x y)) 1 2) => 3 Map (map 'list #'(lambda (x) (1+ x)) (list 0 1 2 3)) => (1 2 3 4)

Multi-Paradigms: Functional Filter => (1 3 5) Fold(foldr in ML) (reduce #'+ (list 1 2 3 4 5)) => 15 Curried Function, Lazy Evaluation, and more...

Multi-Paradigms: Imperative Common Lisp does provide imperative operators like: (setf x 10) x := 10 And for functional functions, Common Lisp also provides their destructive version: map map-into filter (remove-if-not) delete-if-not And even more: goto, for, while...

Multi-Paradigms: OOP Common Lisp has its own implementation for object oriented programming, which is called CLOS. Unlike Java or C++, Common Lisp uses an approach called Generic Function instead of Message Passing. Basically, all the methods belongs to generic function instead of a specific class.

Multi-Paradigms: OOP For example, if there's a human class and there's a method called speak, then I create an instance of human called 'me': In message passing style: me.speak("hello") In generic function style: speak(me, "Hello")

Multi-Paradigms: OOP ;;; CLOS Example (defclass human () ((name :initform "Anonymous" :initarg :name :accessor name))) (defclass man (human) ()) (defclass woman (human) ()) (defgeneric say-love (human) (:documentation "A human says: 'I love you!'")) (defmethod say-love ((obj human)) (format t "~a says: 'I love you!'~%" (name obj))) (defmethod say-love :after ((obj man)) (format t "In ~a's mind: Hmm... Why should I say that?~% (defvar remeo (make-instance 'man :name "Romeo")) (defvar juliet (make-instance 'woman :name "Juliet"))

Multi-Paradigms: OOP A 'men' will have different behavior. CL-USER> (say-love juliet) Juliet says: 'I love you!' NIL CL-USER> (say-love remeo) Romeo says: 'I love you!' In Romeo's mind: Hmm... Why should I say that? NIL It s called method combination.

What Makes Lisp Special? 1. S-Expression 2. Macro

S-Expression In Common Lisp, a s-exp would be like: s-exp : (op s-exp1 s-exp2...) op: a function a macro a special form And interestingly, a s-exp could be made like this: (cons 1 2) => (1. 2) (cons 1 (cons 2 nil)) => (1. (2. nil)) => (1 2) (cons '+ (cons 1 (cons 2 nil))) => (+ 1 2) (eval (cons '+ (cons 1 (cons 2 nil)))) => 3

S-Expression A s-exp looks like a linked list but actually a tree. (car (list '+ 1 2)) => '+ (cdr (list '+ 1 2)) => (1 2) Writing s-expressions is actually writing the Abstract Syntax Tree(AST).

S-Expression: Benefits There will be no lexical analysis because all the codes already are the AST. There will be no need to worry about the Operator Precedence. Very convenient to represent data structures like trees and graphs.

Macro: Why Lisp is Called Programmable Programming Language? Unlike functions, macros will be expanded first before evaluated; After expanding finished, the whole s-expression generated by macro will be evaluated; So a macro is actually a function that transforms arguments to s-expressions.

Macro: A Simple Example In this case, it acts like a inline function. CL-USER> (defun add (x y) (+ x y)) ADD CL-USER> (defmacro add-1 (x y) `(+,x,y)) ADD-1 CL-USER> (add 10 20) 30 CL-USER> (add-1 10 20) 30 CL-USER> (macroexpand '(add-1 10 "string")) (+ 10 "string") T

Macro: A Real (but a little silly) Example Macros can do something that a function can never do. (defmacro delay (thing) ;; return a thunk `(lambda (),thing)) (defun force (thunk) (funcall thunk)) (defmacro my-if (test then else) `(block nil (and,test (return,then)),else)) (defun factorial (n) (my-if (= n 0) ;; test 1 ;; then (* n (factorial (- n 1))))) ;; else

Macro: A Real Example Macros can do something that a function can never do. CL-USER> (delay (loop)) #<FUNCTION (LAMBDA ()) {1003AF4EBB}> CL-USER> (macroexpand '(delay (loop))) #'(LAMBDA () (LOOP)) T CL-USER> (force (delay (+ 1 2))) 3 CL-USER> (factorial 20) 2432902008176640000 CL-USER> (macroexpand '(my-if (= n 0) ; test 1 ; then (* n (factorial (- n 1))))) ; else (BLOCK NIL (AND (= N 0) (RETURN 1)) (* N (FACTORIAL (- N 1 T

Macro: Define New Syntax ;;;; define new syntax by macro (defmacro while (test &body body) `(do () ((not,test)),@body)) (defmacro for (var start end &body body) (let ((gend (gensym))) `(do ((,var,start (1+,var)) (,gend,end)) ((>,var,gend)),@body))) CL-USER> (let ((x 5)) (while (> x 0) (format t "~d " x) (decf x))) 5 4 3 2 1 NIL CL-USER> (for i 1 5 (format t "~d " i)) 1 2 3 4 5 NIL

Macro: Even Let Evaluation Happens During 'Compiling' Time As mentioned before, a macro will be expanded before evaluated, even before compiled. CL-USER> (defmacro avg (&rest numbers) `(/ (+,@numbers),(length numbers))) AVG CL-USER> (avg 1 2 3 4 5 6 7 8 9 10) 11/2 CL-USER> (macroexpand '(avg 1 2 3 4 5 6 7 8 9 10)) (/ (+ 1 2 3 4 5 6 7 8 9 10) 10) T 'Counting how many numbers' happens before run time.

Lisp: An Edge of Programming Languages

Thanks for Watching! We toast the Lisp programmer who pens his thoughts within nests of parentheses. -- Alan J. Perlis <SICP>'s foreword