Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Functional Programming with LISP

7 091 vues

Publié le

Publié dans : Technologie
  • Identifiez-vous pour voir les commentaires

Functional Programming with LISP

  1. 1. Functional Programming with LISt Processing © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> All Rights Reserved.
  2. 2. Introduction© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> All Rights Reserved.
  3. 3. What to Expect?Ws of LISPLanguage SpecificsFun with Recursion © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 3 All Rights Reserved.
  4. 4. What is LISP?Functional Programming Language Conceived by John McCarthy in 1956Name comes from its initial powerful List Processing featuresNatural computation mechanism: RecursionStandardization as Common LISP ANSI released standards in 1996Thought of as for Artificial Intelligence But could pretty much do anythingExamples range from OS, editors, compilers, games, GUIs, andyou think of it © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 4 All Rights Reserved.
  5. 5. Current Available FormsANSI Common Lisp: clisp Compiler, Interpreter, DebuggerGNU Common Lisp: gcl Compiler, InterpreterCMU Common Lisp: cmucl By Carnegie Mellon University Default available as .deb packages Use “alien –to-rpm" to convert them to rpmAllegro CL: Commercial Common Lisp implementation © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 5 All Rights Reserved.
  6. 6. Why LISP?“The programmable programming language"Whats good for the languages designer Is good for the languages usersWish for new features for easier programming? As you can just add the feature yourselfCode the way our brain thinks: Recursive Most natural way of programming50 lines of Code © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 6 All Rights Reserved.
  7. 7. Language SpecificsData StructuresBasic OperationsControl StructuresBasic I/O © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 7 All Rights Reserved.
  8. 8. Data StructuresS-Expression: Atom or ListAtom: String of characters (Values or Variables) Peace, 95432, -rtx, etcList: 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. 9. Basic OperationsLisp Program = Sequence of Functions Applied to their Arguments Returning Lisp ObjectsFunction 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. 10. Basic Operations ...Format: (function arg1 arg2 … argn)Lets try Commands: car, cdr, cons, quote Predicates: atom, nullNB Lisp is case-insensitiveMore: first, last, rest, append, consp, ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 10 All Rights Reserved.
  11. 11. Mathematical OperationsPredicates: zerop, plusp, evenp, integerp, floatpArithmetic: +, -, *, /, rem, 1+, 1-Comparisons: =, /=, <, >, <=, >=Rounding: floor, ceiling, truncate, roundMore Functions max, min, exp, expt, log, abs, signum, sqrt, isqrt © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 11 All Rights Reserved.
  12. 12. Control StructuresConstants & Variables: Atoms w/ & w/o quote ()Assignment: setq, psetq, set, setfConditionals: equal, cond, ifLogical: and, or, notFunctions (defun func-name (par1 … parn) (commands)) Unnamed: (lambda (par1 … parn) (commands)) © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 12 All Rights Reserved.
  13. 13. Lets try some functionsExtract the second element from a listInsert an element at second position in a listChange nth element in a listFind 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. 14. Iteration is Human Recursion is God© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 14 All Rights Reserved.
  15. 15. Recursion For any recursion, we need 2 things Recursive Relation Termination Condition Functional ProceduralRecursive Relation From Mathematics. Tricky Extreme Fairly Simple ConditionsTermination Needs Thought Fairly TrivialCondition Lets try some examples to understand © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 15 All Rights Reserved.
  16. 16. Tracing & AnalysisTracing 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. 17. Tail RecursionBottom most calls return = Topmost calls return Lets observe the trace on list reversalMay not be always possibleBut 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. 18. Example: Set OperationsSets: List of AToms (LATs)Operations: member, union, intersection, adjoinLets write some examples which support sets being elements of set © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 18 All Rights Reserved.
  19. 19. Basic Input / OutputBasic Input (read [input-stream] [eof-error] [eof-value]) → s-exprBasic 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. 20. Loading FilesLoading 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. 21. ReferencesCommon Lisp: http://www.lisp.orgCLISP: http://clisp.cons.orgPractical 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. 22. What all have we learnt?Ws of LISPLanguage Specifics Demonstration Data Structures Basic Operations Control Structures Basic I/OFun with Recursion Power, Tail Recursion, Tracing & Analysis © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 22 All Rights Reserved.
  23. 23. Any Queries?© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 23 All Rights Reserved.