SlideServe
Menu
Préférences
© SlideServe · v2026

Lisp: Basics and Operations in Functional Programming

griffin-collier avatar
18 diapositives · 281 Views · Nov 06, 2014

Learn the fundamentals of Lisp, a functional language similar to others but unique in its recursive nature and list-based structure. Explore prefix notation, list functions, expressions, selection, and iteration with examples. Master recursion and function definitions in Lisp.

Lisp: Basics and Operations in Functional Programming - slide 1
01 / 18Lisp: Basics and Operations in Functional Programming
1 / 17

Télécharger la présentation

Lisp: Basics and Operations in Functional Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Lisp A functional language

  2. As always… How is it similar? • First it runs on the same OS as all applications • Uses runtime activation stack as others • Needs sequence/selection/iteration • Needs organization/procedures • Just not obvious where these things are

  3. Fundamentals • First, everything is a list • Second there are a few important fundamentals/operations to grasp • Third it is fundamentally recursive (as are lists .. they have sublists) • It is functional not procedural. Everything is viewed as either defining or invoking functions

  4. A Lisp List ( A B C D) bounded by parentheses separated by spaces

  5. A Lisp List Lists are composed of atoms ( A B C D) OR sublists ( (A) B (C D))

  6. Basic Operationsuse PREFIX notationoperator precedes operands Abs(x) operator operand ( + 2 4 )

  7. Other simple examples ( + 2 4 ) 6 ( - 2 4 ) -2 ( + 2 – 7 4 ) 2+(7-4)=5

  8. Prefix notation simplifies • Prefix looks more complicated • …BUT • Think about precedence and associativity • It’s NOT relevant to prefix format • No precedence or associativity rules

  9. Prefix notation simplifies In standard algebra ( 10 / 5 + 7 – 2 * 2 ) Need to know precedence rules In lisp (– + / 10 5 7 * 2 2 ) No rules to know … Well, use a single stack, push each operator or operand, Evaluate whenever two operands on the top. Example on next slide.

  10. Prefix notation simplifies (– + / 10 5 7 * 2 2 ) – + – ( + / 10 5 7 * 2 2 ) / + – ( / 10 5 7 * 2 2 ) 10 / + – (10 5 7 * 2 2 ) (5 7 * 2 2 ) 5 10 / + – 2 + – 7 2 + – (7 * 2 2 ) 9 – (* 2 2 ) * 9 – (2 2 ) 2 * 9 – (2) 2 2 * 9 – 4 9 – () 5 Shows where 2 operands on top… evaluate!

  11. Expressions • Substitute one algorithm that is new for all associativity and precedence rules • Granted it is odd at this point. • Only emphasizing the that precedence are a consequence of infix notation. • Not absolutely necessary in all notations. • Like postfix or prefix and variations

  12. Main Lisp list functions car -> returns first element in the list cdr -> returns all but the first element in the list

  13. Main Lisp list functions ( car ( A B C D)) = A ( cdr ( A B C D)) = ( B C D) (car ((A) B (C D))) = (A) (cdr ((A) B (C D))) = (B (C D))

  14. You know to • Do expressions • Call and evaluate functions • Do some simple list functions • How about • Selection • Iteration

  15. Selection ( cond ( ( > a b ) 1) ( ( > c d ) 2 ) ( ( > e f ) 3 ) ( ELSE 4) ) if (a > b) return 1 else if ( c > d) return 2 else if ( e > f) return 3 else return 4 In lisp

  16. Iteration • Use recursion • Before seeing recursion • Learn how to define a function • Learn how to define a recursive function

  17. Defining a function(adding two integers) ( define ( Addx y ) ( + x y ) ) In lisp Add (x, y) { return x + y; } Works for two simple atoms, but what about lists? You need recursion!

  18. Iteration Summing elements in a list(table) int Addup (a,n) { sum=0; for (i=0, i<n; i++) sum+= a[i]; return sum; } int Addup (a,n) { sum=0; if (n==1) return a[0]; else return a[0]+Addup(a[1],n-1) } Recursively in c in lisp ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) ) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work!