Weitere Ă€hnliche Inhalte Ăhnlich wie Functional Programming with LISP (20) Mehr von Anil Kumar Pugalia (9) KĂŒrzlich hochgeladen (20) Functional Programming with LISP1. Functional Programming
with
LISt Processing
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com>
All Rights Reserved.
3. What to Expect?
W's of LISP
Language Specifics
Fun with Recursion
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 3
All Rights Reserved.
4. What is LISP?
Functional Programming Language
Conceived by John McCarthy in 1956
Name comes from its initial powerful List Processing features
Natural computation mechanism: Recursion
Standardization as Common LISP
ANSI released standards in 1996
Thought of as for Artificial Intelligence
But could pretty much do anything
Examples range from OS, editors, compilers, games, GUIs, and
you think of it
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 4
All Rights Reserved.
5. Current Available Forms
ANSI Common Lisp: clisp
Compiler, Interpreter, Debugger
GNU Common Lisp: gcl
Compiler, Interpreter
CMU Common Lisp: cmucl
By Carnegie Mellon University
Default available as .deb packages
Use âalien âto-rpm" to convert them to rpm
Allegro CL: Commercial Common Lisp implementation
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 5
All Rights Reserved.
6. Why LISP?
âThe programmable programming language"
What's good for the language's designer
Is good for the language's users
Wish for new features for easier programming?
As you can just add the feature yourself
Code the way our brain thinks: Recursive
Most natural way of programming
50 lines of Code
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 6
All Rights Reserved.
8. Data Structures
S-Expression: Atom or List
Atom: String of characters ('Values or Variables)
Peace, 95432, -rtx, etc
List: Collection of S-Expressions enclosed by ()
(The 100 times done)
(Guava (43 (2.718 5) 56) Apple)
What is a Null List: ()?
Common Pitfall: Lists need to start with '
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 8
All Rights Reserved.
9. Basic Operations
Lisp Program = Sequence of Functions
Applied to their Arguments
Returning Lisp Objects
Function Types
Predicate
Tests conditions with its arguments
Returns Nil (FALSE) or anything else (TRUE)
Command
Performs operation with its arguments
Returns an S-Expression
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 9
All Rights Reserved.
10. Basic Operations ...
Format: (function arg1 arg2 ⊠argn)
Let's try
Commands: car, cdr, cons, quote
Predicates: atom, null
NB Lisp is case-insensitive
More: first, last, rest, append, consp, ...
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 10
All Rights Reserved.
11. Mathematical Operations
Predicates: zerop, plusp, evenp, integerp, floatp
Arithmetic: +, -, *, /, rem, 1+, 1-
Comparisons: =, /=, <, >, <=, >=
Rounding: floor, ceiling, truncate, round
More Functions
max, min, exp, expt, log, abs, signum, sqrt, isqrt
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 11
All Rights Reserved.
12. Control Structures
Constants & Variables: Atoms w/ & w/o quote (')
Assignment: setq, psetq, set, setf
Conditionals: equal, cond, if
Logical: and, or, not
Functions
(defun func-name (par1 ⊠parn) (commands))
Unnamed: (lambda (par1 ⊠parn) (commands))
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 12
All Rights Reserved.
13. Let's try some functions
Extract the second element from a list
Insert an element at second position in a list
Change nth element in a list
Find length of a list (iteratively)
Find variance of a list of elements
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 13
All Rights Reserved.
14. Iteration is Human
Recursion is God
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 14
All Rights Reserved.
15. Recursion
For any recursion, we need 2 things
Recursive Relation
Termination Condition
Functional Procedural
Recursive Relation From Mathematics. Tricky Extreme
Fairly Simple Conditions
Termination Needs Thought Fairly Trivial
Condition
Let's try some examples to understand
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 15
All Rights Reserved.
16. Tracing & Analysis
Tracing Recursive Function Calls
Enable tracing: (trace recursive-func)
Invoke the function to be traced: (recursive-func âŠ)
Performance Analysis
(time (func âŠ))
Samples with our recursive functions
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 16
All Rights Reserved.
17. Tail Recursion
Bottom most call's return = Topmost call's return
Let's observe the trace on list reversal
May not be always possible
But if possible, it is a smart compiler advantage
Cuts-off processing as soon as lowest level returns
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 17
All Rights Reserved.
18. Example: Set Operations
Sets: List of AToms (LATs)
Operations: member, union, intersection, adjoin
Let's write some examples
which support sets being elements of set
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 18
All Rights Reserved.
19. Basic Input / Output
Basic Input
(read [input-stream] [eof-error] [eof-value]) â s-expr
Basic Output
(print s-expr [output-stream]) â s-expr
(format destination control-string [args])
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 19
All Rights Reserved.
20. Loading Files
Loading Lisp file for interpretation
(load "file.lisp")
Compiling a Lisp file
(compile "file.lisp") or (compile "file")
Loading a compiled Lisp file
(load (compile "file.lisp"))
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 20
All Rights Reserved.
21. References
Common Lisp: http://www.lisp.org
CLISP: http://clisp.cons.org
Practical Common Lisp by Peter Seibel
Also @ http://www.gigamonkeys.com/book/
LISP Tutorial: http://www.mars.cs.unp.ac.za/lisp/
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 21
All Rights Reserved.
22. What all have we learnt?
W's of LISP
Language Specifics Demonstration
Data Structures
Basic Operations
Control Structures
Basic I/O
Fun with Recursion
Power, Tail Recursion, Tracing & Analysis
© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 22
All Rights Reserved.