Introduction To Lisp

K
kyleburtonApplication Architecture Manager
Why Won’t Lisp Just Go
      Away?
       Inroducing Lisp
      50 Years Young (2008)
http://lisperati.org/casting.html, by permission of Conrad Barski, MD
Effort vs Productivity
                   (my subjective point of view)
                                 (not to scale)
                     (this chart has no meaning, it is a joke)

100


                   PAIP, On Lisp, SICP



 75




                  The segafault that
                  made you leave C++
 50



      Java Cookbook                                                          EJB3, J5EE, JMX,
                                                                        Me   Struts, JMS,
                                                                             Exhausted Permgen
 25




                                                                             You are Here

  0


           Java                          C++                     Lisp
Overview
 What is Lisp?
 How to get started
   Some Basics
   Some Common Recipes
 Wet your appetite for more
   Not so basic things
 Where to go next
What is Lisp?
 A Programming Language
 Brought to you by John McCarthy circa 1958
 Resistance is Futile...
    Garbage Collection, Closures, Functional, Object
    Oriented, AOP, DSLs, Conditions and Restarts,
    Continuations, what else ya got?
Welcome to the Family
 Common Lisp (Lisp 2)
    LispWorks, Alegro

    OpenMCL, SBCL, Clisp, ABCL and others

 Scheme (Lisp 1) by Sussman and Steele
    PLT, Chicken, Bigloo, Clojure, SISC, JScheme, and
    others
 Less Common
    Dylan, Qi, Termite, ECL, Arc, and others...
Quick Start
LispBox
  http://gigamonkeys.com/lispbox/
  Windows, OS X, Linux

Ready Lisp
  http://www.newartisans.com/software/readylisp.html

  OS X
Roll your own
 Emacs
 SLIME
   The Superior Lisp Interaction Mode for Emacs
 Common Lisp
   SBCL, CLISP
 paraedit-el
Basics

 Parentheses, lots and lots of parentheses
 Everything is an Expression
 Prefix Notation
 Lists, ‘cons’, pair
   Singly Linked List
Basics: Cons Cells, Lists
car
                          (), nil, the empty list, false
          cdr



 1                  (list 1) (cons 1 nil)



 1    2             (cons 1 2)



 1              2         3              (list 1 2 3)

 (cons 1 (cons 2 (cons 3 nil)))
Basics: Expressions
(if test-p consequent otherwise)

(+ 1 2 3 4 5)   => 15

(let ((x 3)
      (y 2))
  (* x y))      => 6

(expt 2 42)     => 4398046511104
Basics: Functions
(defun test (n)
  (cond
    ((= n 0)
	    (+ 1 n))
	   (t
	    (- n 1))))

(funcall #'test 3)
(apply #’test ‘(3))

(mapcar #'test '(-2 -1 0 1 2))
=> (-3 -2 1 0 1)
Basics: Assignment
 (setf x '(a b c))
 => (A B C)

 (setf (car x) 1)
 => (1 B C)

 (setf (second x) 2)
 => (1 2 C)
Numeric Tower

 Ints and Floats, but also...
 Ratios
 Built in Bignum Support
 Built in Complex Numbers
Numeric Tower
1/2
(/ 1 3)               => 1/3
(* 33333 (/ 1 33333)) => 1


(expt 2 100)          => 1267650600228229401496703205376


(* #c(0 1) #c(0 1))   => -1
Familiar Data Structures
 Vectors (arrays)
 Hash Tables (map)
 Structures (records)
 Objects - CLOS
 Sequence Types (and streams)
Library System: ASDF
 ASDF - another system definition facility
   Like Ruby’s gem or Perl’s CPAN (but inscrutable)
 (require :asdf-install)
 (asdf-install:install :cl-ppcre)
 Repository: cliki.net
Example: Fetch the Web

(require :drakma)
(use-package :drakma)
(defvar content nil)
(setf content
  (http-request quot;http://google.com/quot;))
(format t quot;len:~a~" (length content))
(format t quot;~a~" (subseq content 0 100))
Example: Serve the Web
(require :hunchentoot)
(use-package :hunchentoot)
(setq *dispatch-table*
  (list
    (create-prefix-dispatcher
      quot;/indexquot; 'index-page)))
(defun index-page ()
  quot;<h1>Hunchentoot Demo</h1>.quot;)
(defvar server
  (hunchentoot:start-server :port 4242))
Example: Process Files

(require :cl-ppcre)

(with-open-file (inp quot;.../input.txtquot; :direction :input)
  (loop for line = (read-line inp nil nil)
     while line
     do
     (format t quot;~a~&quot;
	       (cl-ppcre:regex-replace quot;lis+pquot; line quot;lispquot;))))
Example: Unit Testing
(define-test test-flatten
 (assert-false (flatten))
 (assert-false (flatten '()))
 (assert-equal '(a)
               (flatten 'a))
 (assert-equal '(a)
               (flatten '(a)))
 (assert-equal '(a b c d)
               (flatten '(a (b (c (d)))))))
More Interesting Stuff

 Macros, Reader Macros, syntax-rules
 Restarts
   “To Err is Expected, To Recover, Divine” -- David B. Lamkins

 Functional Programming
 Pattern Matching (Unification)
What’s a Macro?
  A tool for Abstraction
  Code Generator
  Compiler


  defmacro
  define-macro
  syntax-rules
Macro: random-if

(defmacro random-if (prob consequent otherwise)
  `(if (< (random 1.0) ,prob))
       ,consequent
       ,otherwise))

(random-if 1/2
      (format t quot;went left~&quot;)
      (format t quot;went right~&quot;)
Macro: aprog1
(defmacro aprog1 (it &rest body)
  `(let ((it ,it))      give the thing   a name
     ,@body
                      let them use it
     it))
                 return it

(aprog1 (make-hash-table :test #’equal)
  (setf (gethash “a” it) 1)
  (setf (gethash “b” it) 2))
=> #<HASH-TABLE :TEST EQUAL :COUNT 2 {BE49831}>
Macro: rcut
(defmacro rcut (&rest body)
 (let* ((formals (list))
        (new-body
         (map-tree #'(lambda (elt)
                       (if (symbol-equal '<> elt)
                           (aprog1
                            (gensym)
                            (push it formals)
                            it)
                           elt))
                   body)))
   `#'(lambda ,(reverse formals)
        ,@new-body)))

(macroexpan-1 ‘(rcut (format “val:~a~&” <>)))
  => #'(LAMBDA (#:G2348) (FORMAT NIL quot;elt:~a~&quot; #:G2348))
Restarts: Structured Recovery
(defun file-size (file)
  (restart-case
    (if (not (probe-file file))       ‘throw’
      (error 'file-not-found)
      (sb-posix:stat-size (sb-posix:stat file)))
    (file-not-found (new-file-size)
      :report quot;Specify missing size.quot;
                                             recovery
      :interactive read-new-value
      new-file-size)))

(defun bytes-used (files)
  (loop for file in files
	   summing (file-size file)))
Restarts in action
CL-USER> (format t quot;bytes:~a~&quot; (bytes-used '(quot;/etc/passwdquot; quot;/foo/barquot;)))
Condition FILE-NOT-FOUND was signalled.
   [Condition of type FILE-NOT-FOUND]

Restarts:
 0: [FILE-NOT-FOUND] Specify missing size.
 1: [ABORT] Return to SLIME's top level.
 2: [TERMINATE-THREAD] Terminate this thread (#<THREAD quot;repl-threadquot; ...>)

Backtrace:
  0: (FILE-SIZE quot;/foo/barquot;)
  1: (BYTES-USED (quot;/etc/passwdquot; quot;/foo/barquot;))
   ...snip...
Enter a new value: 12
bytes:2900
More Interesting Stuff

 Functional Programming
   Functions are first class - composable
   list comprehensions (cl-incf)
   pattern-matching (cl-unification, pcond)
 DSLs, mini-languages
First Class Functions
(mapcar #'(lambda (x) (+ 2 x))
          '(1 2 3 4))       => ‘(3 4 5 6)

(defun curry (fn &rest args)
  #'(lambda (&rest rest)
      (apply fn (append args rest))))

(funcall (curry #'+ 2) 3)   => 5

(mapcar (curry #'+ 2)
    '(1 2 3 4))             => ‘(3 4 5 6)
First Class Functions

 (defun memoize (fn)
   (let ((cache (make-hash-table :test #'equal)))
      #’(lambda (&rest args)
         (let ((key (format nil quot;~aquot; args)))
 	        (if (gethash key cache)
 	
Return      (gethash key cache)
from
 	
cache       (aprog1
 	            (apply fn args)
   	          (setf (gethash key cache) it)))))))

Add result to
cache and return
First Class Functions
(defun myfn (a b c)
  (format t quot;called: a:~a b:~a c:~a~&quot; a b c)
  (+ a b c))

(let ((mfn (memoize #'myfn))
      (args '(1 2 3))
      (args2 '(4 5 6)))
  (format t quot;mfn ~a: ~a~&quot; args (apply mfn args))
  (format t quot;mfn ~a: ~a~&quot; args (apply mfn args))
  (format t quot;mfn ~a: ~a~&quot; args2 (apply mfn args2))
  (format t quot;mfn ~a: ~a~&quot; args2 (apply mfn args2)))
=>
called: a:1 b:2 c:3        not in cache
mfn (1 2 3): 6
mfn (1 2 3): 6             in cache
called: a:4 b:5 c:6        not in cache
mfn (4 5 6): 15
mfn (4 5 6): 15            in cache
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions
(defun lcmp-qsort (things)
  (cond ((null things)
	 things)
	 (t
	 (destructuring-bind (pivot . tl) things
	    (append
	     (lcmp-qsort
	       (assemble x (<- x tl) (< x pivot)))
	     (list pivot)
	     (lcmp-qsort
	       (assemble x (<- x tl) (>= x pivot))))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
List Comprehensions

(defun all-permutations (things)
  (cond
    ((= 1 (length things))
     (list things))
    (t
     (assemble
	       (cons Head Tail)
        (<- Head things)
        (<- Tail
            (all-permutations (remove Head things)))))))
Pattern Matching: pcond
(defun ptest (names)
  (pcond:pcond
   ((and (:pat ((?first ?last) . ?rest)
	          names)
	        (:re quot;(.+)oquot; ?first (before-o)))
    (format t quot;before-o: ~a~&quot; before-o)
    (ptest ?rest))))

(ptest '((quot;George quot; quot;Washingtonquot;) (quot;Johnquot; quot;Adamsquot;)))

=> before-o: Ge
=> before-o: J
DSLs, mini-languages
(define-syntax aprog1
  (syntax-rules (it)
    ((_ expression . body)
     (let ((it expression))
       body
       it))))

(macroexpand '(aprog1 (list) (set! it (cons ‘a it))))
=>
(let ((it (list)))
   (set! it (cons 'a it))
   it)
DSLs, mini-languages

(define m
  (automation init
    (init : (c -> more))
    (more : (a -> more)
            (d -> more)
            (r -> end))
    (end :)))
DSLs, mini-languages
(define-syntax automation
  (syntax-rules (: ->)
    ((_ init-state
	     (state : (label -> target) ...)
	     ...)
     (letrec
       ((state
	          (lambda (stream)
	 	          (cond
	 	            ((empty? stream) #t)
	 	            (else
	 	              (case (first stream)
	 	                ((label) (target (rest stream)))
	 	                ...
	 	                (else #f))))))
	      ...)
       init-state))))
DSLs, mini-languages
(letrec
  ((init (lambda (GenTemp%2)
           (cond ((empty? GenTemp%2) #t)
                 (else (case (first GenTemp%2)
                         ((c) (more (rest GenTemp%2)))
                         (else #f))))))
   (more (lambda (GenTemp%2)
           (cond ((empty? GenTemp%2) #t)
                 (else (case (first GenTemp%2)
                         ((a) (more (rest GenTemp%2)))
                         ((d) (more (rest GenTemp%2)))
                         ((r) (end (rest GenTemp%2)))
                         (else #f))))))
   (end (lambda (GenTemp%2)
           (cond ((empty? GenTemp%2) #t)
                 (else (case (first GenTemp%2)
                         (else #f)))))))
  init)
That’s the overview




 http://lisperati.org/casting.html, by permission of Conrad Barski, MD
Now where am I supposed to go?
 On-Line Resources

   Common Lisp Hyper Spec

   On Lisp (Paul Graham)

   Practical Common Lisp (Peter Siebel)

   Cliki.net

   common-lisp.net

   JRMs Syntax Rules Primer

   Casting Spells with Lisp: http://lisperati.org/casting.html

   http://del.icio.us/mortis/lisp
Now where am I supposed to go?

 Books
  PAIP (Peter Norvig)
  Practical Common Lisp (Peter Seibel)
  SICP (Ableson and Sussman)
  Common Lisp the Language (Guy Steele)
  ANSI Common Lisp (Paul Graham)
1 of 52

Recommended

(Ai lisp) by
(Ai lisp)(Ai lisp)
(Ai lisp)Ravi Rao
719 views26 slides
Advance LISP (Artificial Intelligence) by
Advance LISP (Artificial Intelligence) Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) wahab khan
911 views26 slides
Lisp by
LispLisp
Lispsonukumar142
1.5K views57 slides
A brief introduction to lisp language by
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp languageDavid Gu
1.6K views22 slides
Lisp by
LispLisp
LispAniruddha Chakrabarti
9.4K views44 slides
INTRODUCTION TO LISP by
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISPNilt1234
441 views124 slides

More Related Content

What's hot

Gentle Introduction To Lisp by
Gentle Introduction To LispGentle Introduction To Lisp
Gentle Introduction To LispDamien Garaud
1K views27 slides
Lisp by
LispLisp
LispFraboni Ec
780 views23 slides
Lisp Programming Languge by
Lisp Programming LangugeLisp Programming Languge
Lisp Programming LangugeYaser Jaradeh
2.9K views26 slides
Prolog & lisp by
Prolog & lispProlog & lisp
Prolog & lispIsmail El Gayar
16.5K views24 slides
Learn a language : LISP by
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
3.2K views34 slides
Lisp by
LispLisp
Lisphuzaifa ramzan
794 views35 slides

What's hot(20)

Gentle Introduction To Lisp by Damien Garaud
Gentle Introduction To LispGentle Introduction To Lisp
Gentle Introduction To Lisp
Damien Garaud1K views
Lisp Programming Languge by Yaser Jaradeh
Lisp Programming LangugeLisp Programming Languge
Lisp Programming Languge
Yaser Jaradeh2.9K views
Learn a language : LISP by Devnology
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
Devnology3.2K views
Big picture of category theory in scala with deep dive into contravariant and... by Piotr Paradziński
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
Piotr Paradziński840 views
Scala Back to Basics: Type Classes by Tomer Gabel
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel3.7K views
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics by John De Goes
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes7.2K views
BayFP: Concurrent and Multicore Haskell by Bryan O'Sullivan
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
Bryan O'Sullivan2.3K views
Haskell for data science by John Cant
Haskell for data scienceHaskell for data science
Haskell for data science
John Cant2.5K views
Lambda выражения и Java 8 by Alex Tumanoff
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8
Alex Tumanoff4.3K views

Similar to Introduction To Lisp

CL metaprogramming by
CL metaprogrammingCL metaprogramming
CL metaprogrammingdudarev
456 views12 slides
The Magnificent Seven by
The Magnificent SevenThe Magnificent Seven
The Magnificent SevenMike Fogus
631 views16 slides
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers by
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate CompilersFunctional Thursday
374 views20 slides
Scala @ TomTom by
Scala @ TomTomScala @ TomTom
Scala @ TomTomEric Bowman
1.3K views42 slides
Continuation Passing Style and Macros in Clojure - Jan 2012 by
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Leonardo Borges
3.7K views80 slides
Functional Concepts for OOP Developers by
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developersbrweber2
29.8K views107 slides

Similar to Introduction To Lisp(20)

CL metaprogramming by dudarev
CL metaprogrammingCL metaprogramming
CL metaprogramming
dudarev456 views
The Magnificent Seven by Mike Fogus
The Magnificent SevenThe Magnificent Seven
The Magnificent Seven
Mike Fogus631 views
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers by Functional Thursday
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
Scala @ TomTom by Eric Bowman
Scala @ TomTomScala @ TomTom
Scala @ TomTom
Eric Bowman1.3K views
Continuation Passing Style and Macros in Clojure - Jan 2012 by Leonardo Borges
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
Leonardo Borges3.7K views
Functional Concepts for OOP Developers by brweber2
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
brweber229.8K views
Clojure for Java developers - Stockholm by Jan Kronquist
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
Jan Kronquist1.7K views
Meta-objective Lisp @名古屋 Reject 会議 by dico_leque
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議
dico_leque940 views
Lisp Macros in 20 Minutes (Featuring Clojure) by Phil Calçado
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Phil Calçado7.8K views
Groovy by Zen Urban
GroovyGroovy
Groovy
Zen Urban1.1K views
Scala as a Declarative Language by vsssuresh
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
vsssuresh3.2K views
Clojure Intro by thnetos
Clojure IntroClojure Intro
Clojure Intro
thnetos10.5K views
Scala + WattzOn, sitting in a tree.... by Raffi Krikorian
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....
Raffi Krikorian1.6K views
please rewrite the correct code and do not use set ! there are many e.docx by JakeT2gGrayp
please rewrite the correct code and  do not use set ! there are many e.docxplease rewrite the correct code and  do not use set ! there are many e.docx
please rewrite the correct code and do not use set ! there are many e.docx
JakeT2gGrayp12 views
Modern technologies in data science by Chucheng Hsieh
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
Chucheng Hsieh3K views
Class 16: Making Loops by David Evans
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making Loops
David Evans618 views

Recently uploaded

Optimizing Communication to Optimize Human Behavior - LCBM by
Optimizing Communication to Optimize Human Behavior - LCBMOptimizing Communication to Optimize Human Behavior - LCBM
Optimizing Communication to Optimize Human Behavior - LCBMYaman Kumar
38 views49 slides
The Power of Heat Decarbonisation Plans in the Built Environment by
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built EnvironmentIES VE
84 views20 slides
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...ShapeBlue
164 views13 slides
"Running students' code in isolation. The hard way", Yurii Holiuk by
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk Fwdays
36 views34 slides
Transcript: Redefining the book supply chain: A glimpse into the future - Tec... by
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...BookNet Canada
41 views16 slides
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 by
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023BookNet Canada
44 views19 slides

Recently uploaded(20)

Optimizing Communication to Optimize Human Behavior - LCBM by Yaman Kumar
Optimizing Communication to Optimize Human Behavior - LCBMOptimizing Communication to Optimize Human Behavior - LCBM
Optimizing Communication to Optimize Human Behavior - LCBM
Yaman Kumar38 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE84 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue164 views
"Running students' code in isolation. The hard way", Yurii Holiuk by Fwdays
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk
Fwdays36 views
Transcript: Redefining the book supply chain: A glimpse into the future - Tec... by BookNet Canada
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
BookNet Canada41 views
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 by BookNet Canada
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
BookNet Canada44 views
The Power of Generative AI in Accelerating No Code Adoption.pdf by Saeed Al Dhaheri
The Power of Generative AI in Accelerating No Code Adoption.pdfThe Power of Generative AI in Accelerating No Code Adoption.pdf
The Power of Generative AI in Accelerating No Code Adoption.pdf
Saeed Al Dhaheri39 views
Initiating and Advancing Your Strategic GIS Governance Strategy by Safe Software
Initiating and Advancing Your Strategic GIS Governance StrategyInitiating and Advancing Your Strategic GIS Governance Strategy
Initiating and Advancing Your Strategic GIS Governance Strategy
Safe Software184 views
The Role of Patterns in the Era of Large Language Models by Yunyao Li
The Role of Patterns in the Era of Large Language ModelsThe Role of Patterns in the Era of Large Language Models
The Role of Patterns in the Era of Large Language Models
Yunyao Li91 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays33 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue208 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu437 views
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue129 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue196 views
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by Priyanka Aash
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOs
Priyanka Aash162 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue141 views
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue152 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays58 views

Introduction To Lisp

  • 1. Why Won’t Lisp Just Go Away? Inroducing Lisp 50 Years Young (2008)
  • 3. Effort vs Productivity (my subjective point of view) (not to scale) (this chart has no meaning, it is a joke) 100 PAIP, On Lisp, SICP 75 The segafault that made you leave C++ 50 Java Cookbook EJB3, J5EE, JMX, Me Struts, JMS, Exhausted Permgen 25 You are Here 0 Java C++ Lisp
  • 4. Overview What is Lisp? How to get started Some Basics Some Common Recipes Wet your appetite for more Not so basic things Where to go next
  • 5. What is Lisp? A Programming Language Brought to you by John McCarthy circa 1958 Resistance is Futile... Garbage Collection, Closures, Functional, Object Oriented, AOP, DSLs, Conditions and Restarts, Continuations, what else ya got?
  • 6. Welcome to the Family Common Lisp (Lisp 2) LispWorks, Alegro OpenMCL, SBCL, Clisp, ABCL and others Scheme (Lisp 1) by Sussman and Steele PLT, Chicken, Bigloo, Clojure, SISC, JScheme, and others Less Common Dylan, Qi, Termite, ECL, Arc, and others...
  • 7. Quick Start LispBox http://gigamonkeys.com/lispbox/ Windows, OS X, Linux Ready Lisp http://www.newartisans.com/software/readylisp.html OS X
  • 8. Roll your own Emacs SLIME The Superior Lisp Interaction Mode for Emacs Common Lisp SBCL, CLISP paraedit-el
  • 9. Basics Parentheses, lots and lots of parentheses Everything is an Expression Prefix Notation Lists, ‘cons’, pair Singly Linked List
  • 10. Basics: Cons Cells, Lists car (), nil, the empty list, false cdr 1 (list 1) (cons 1 nil) 1 2 (cons 1 2) 1 2 3 (list 1 2 3) (cons 1 (cons 2 (cons 3 nil)))
  • 11. Basics: Expressions (if test-p consequent otherwise) (+ 1 2 3 4 5) => 15 (let ((x 3) (y 2)) (* x y)) => 6 (expt 2 42) => 4398046511104
  • 12. Basics: Functions (defun test (n) (cond ((= n 0) (+ 1 n)) (t (- n 1)))) (funcall #'test 3) (apply #’test ‘(3)) (mapcar #'test '(-2 -1 0 1 2)) => (-3 -2 1 0 1)
  • 13. Basics: Assignment (setf x '(a b c)) => (A B C) (setf (car x) 1) => (1 B C) (setf (second x) 2) => (1 2 C)
  • 14. Numeric Tower Ints and Floats, but also... Ratios Built in Bignum Support Built in Complex Numbers
  • 15. Numeric Tower 1/2 (/ 1 3) => 1/3 (* 33333 (/ 1 33333)) => 1 (expt 2 100) => 1267650600228229401496703205376 (* #c(0 1) #c(0 1)) => -1
  • 16. Familiar Data Structures Vectors (arrays) Hash Tables (map) Structures (records) Objects - CLOS Sequence Types (and streams)
  • 17. Library System: ASDF ASDF - another system definition facility Like Ruby’s gem or Perl’s CPAN (but inscrutable) (require :asdf-install) (asdf-install:install :cl-ppcre) Repository: cliki.net
  • 18. Example: Fetch the Web (require :drakma) (use-package :drakma) (defvar content nil) (setf content (http-request quot;http://google.com/quot;)) (format t quot;len:~a~&quot; (length content)) (format t quot;~a~&quot; (subseq content 0 100))
  • 19. Example: Serve the Web (require :hunchentoot) (use-package :hunchentoot) (setq *dispatch-table* (list (create-prefix-dispatcher quot;/indexquot; 'index-page))) (defun index-page () quot;<h1>Hunchentoot Demo</h1>.quot;) (defvar server (hunchentoot:start-server :port 4242))
  • 20. Example: Process Files (require :cl-ppcre) (with-open-file (inp quot;.../input.txtquot; :direction :input) (loop for line = (read-line inp nil nil) while line do (format t quot;~a~&quot; (cl-ppcre:regex-replace quot;lis+pquot; line quot;lispquot;))))
  • 21. Example: Unit Testing (define-test test-flatten (assert-false (flatten)) (assert-false (flatten '())) (assert-equal '(a) (flatten 'a)) (assert-equal '(a) (flatten '(a))) (assert-equal '(a b c d) (flatten '(a (b (c (d)))))))
  • 22. More Interesting Stuff Macros, Reader Macros, syntax-rules Restarts “To Err is Expected, To Recover, Divine” -- David B. Lamkins Functional Programming Pattern Matching (Unification)
  • 23. What’s a Macro? A tool for Abstraction Code Generator Compiler defmacro define-macro syntax-rules
  • 24. Macro: random-if (defmacro random-if (prob consequent otherwise) `(if (< (random 1.0) ,prob)) ,consequent ,otherwise)) (random-if 1/2 (format t quot;went left~&quot;) (format t quot;went right~&quot;)
  • 25. Macro: aprog1 (defmacro aprog1 (it &rest body) `(let ((it ,it)) give the thing a name ,@body let them use it it)) return it (aprog1 (make-hash-table :test #’equal) (setf (gethash “a” it) 1) (setf (gethash “b” it) 2)) => #<HASH-TABLE :TEST EQUAL :COUNT 2 {BE49831}>
  • 26. Macro: rcut (defmacro rcut (&rest body)  (let* ((formals (list))         (new-body          (map-tree #'(lambda (elt)                        (if (symbol-equal '<> elt)                            (aprog1                             (gensym)                             (push it formals)                             it)                            elt))                    body)))    `#'(lambda ,(reverse formals)         ,@new-body))) (macroexpan-1 ‘(rcut (format “val:~a~&” <>))) => #'(LAMBDA (#:G2348) (FORMAT NIL quot;elt:~a~&quot; #:G2348))
  • 27. Restarts: Structured Recovery (defun file-size (file) (restart-case (if (not (probe-file file)) ‘throw’ (error 'file-not-found) (sb-posix:stat-size (sb-posix:stat file))) (file-not-found (new-file-size) :report quot;Specify missing size.quot; recovery :interactive read-new-value new-file-size))) (defun bytes-used (files) (loop for file in files summing (file-size file)))
  • 28. Restarts in action CL-USER> (format t quot;bytes:~a~&quot; (bytes-used '(quot;/etc/passwdquot; quot;/foo/barquot;))) Condition FILE-NOT-FOUND was signalled. [Condition of type FILE-NOT-FOUND] Restarts: 0: [FILE-NOT-FOUND] Specify missing size. 1: [ABORT] Return to SLIME's top level. 2: [TERMINATE-THREAD] Terminate this thread (#<THREAD quot;repl-threadquot; ...>) Backtrace: 0: (FILE-SIZE quot;/foo/barquot;) 1: (BYTES-USED (quot;/etc/passwdquot; quot;/foo/barquot;)) ...snip... Enter a new value: 12 bytes:2900
  • 29. More Interesting Stuff Functional Programming Functions are first class - composable list comprehensions (cl-incf) pattern-matching (cl-unification, pcond) DSLs, mini-languages
  • 30. First Class Functions (mapcar #'(lambda (x) (+ 2 x)) '(1 2 3 4)) => ‘(3 4 5 6) (defun curry (fn &rest args) #'(lambda (&rest rest) (apply fn (append args rest)))) (funcall (curry #'+ 2) 3) => 5 (mapcar (curry #'+ 2) '(1 2 3 4)) => ‘(3 4 5 6)
  • 31. First Class Functions (defun memoize (fn) (let ((cache (make-hash-table :test #'equal))) #’(lambda (&rest args) (let ((key (format nil quot;~aquot; args))) (if (gethash key cache) Return (gethash key cache) from cache (aprog1 (apply fn args) (setf (gethash key cache) it))))))) Add result to cache and return
  • 32. First Class Functions (defun myfn (a b c) (format t quot;called: a:~a b:~a c:~a~&quot; a b c) (+ a b c)) (let ((mfn (memoize #'myfn)) (args '(1 2 3)) (args2 '(4 5 6))) (format t quot;mfn ~a: ~a~&quot; args (apply mfn args)) (format t quot;mfn ~a: ~a~&quot; args (apply mfn args)) (format t quot;mfn ~a: ~a~&quot; args2 (apply mfn args2)) (format t quot;mfn ~a: ~a~&quot; args2 (apply mfn args2))) => called: a:1 b:2 c:3 not in cache mfn (1 2 3): 6 mfn (1 2 3): 6 in cache called: a:4 b:5 c:6 not in cache mfn (4 5 6): 15 mfn (4 5 6): 15 in cache
  • 33. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 34. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 35. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 36. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 37. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 38. List Comprehensions (defun lcmp-qsort (things) (cond ((null things) things) (t (destructuring-bind (pivot . tl) things (append (lcmp-qsort (assemble x (<- x tl) (< x pivot))) (list pivot) (lcmp-qsort (assemble x (<- x tl) (>= x pivot))))))))
  • 39. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 40. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 41. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 42. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 43. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 44. List Comprehensions (defun all-permutations (things) (cond ((= 1 (length things)) (list things)) (t (assemble (cons Head Tail) (<- Head things) (<- Tail (all-permutations (remove Head things)))))))
  • 45. Pattern Matching: pcond (defun ptest (names) (pcond:pcond ((and (:pat ((?first ?last) . ?rest) names) (:re quot;(.+)oquot; ?first (before-o))) (format t quot;before-o: ~a~&quot; before-o) (ptest ?rest)))) (ptest '((quot;George quot; quot;Washingtonquot;) (quot;Johnquot; quot;Adamsquot;))) => before-o: Ge => before-o: J
  • 46. DSLs, mini-languages (define-syntax aprog1 (syntax-rules (it) ((_ expression . body) (let ((it expression)) body it)))) (macroexpand '(aprog1 (list) (set! it (cons ‘a it)))) => (let ((it (list))) (set! it (cons 'a it)) it)
  • 47. DSLs, mini-languages (define m (automation init (init : (c -> more)) (more : (a -> more) (d -> more) (r -> end)) (end :)))
  • 48. DSLs, mini-languages (define-syntax automation (syntax-rules (: ->) ((_ init-state (state : (label -> target) ...) ...) (letrec ((state (lambda (stream) (cond ((empty? stream) #t) (else (case (first stream) ((label) (target (rest stream))) ... (else #f)))))) ...) init-state))))
  • 49. DSLs, mini-languages (letrec ((init (lambda (GenTemp%2) (cond ((empty? GenTemp%2) #t) (else (case (first GenTemp%2) ((c) (more (rest GenTemp%2))) (else #f)))))) (more (lambda (GenTemp%2) (cond ((empty? GenTemp%2) #t) (else (case (first GenTemp%2) ((a) (more (rest GenTemp%2))) ((d) (more (rest GenTemp%2))) ((r) (end (rest GenTemp%2))) (else #f)))))) (end (lambda (GenTemp%2) (cond ((empty? GenTemp%2) #t) (else (case (first GenTemp%2) (else #f))))))) init)
  • 50. That’s the overview http://lisperati.org/casting.html, by permission of Conrad Barski, MD
  • 51. Now where am I supposed to go? On-Line Resources Common Lisp Hyper Spec On Lisp (Paul Graham) Practical Common Lisp (Peter Siebel) Cliki.net common-lisp.net JRMs Syntax Rules Primer Casting Spells with Lisp: http://lisperati.org/casting.html http://del.icio.us/mortis/lisp
  • 52. Now where am I supposed to go? Books PAIP (Peter Norvig) Practical Common Lisp (Peter Seibel) SICP (Ableson and Sussman) Common Lisp the Language (Guy Steele) ANSI Common Lisp (Paul Graham)