1 / 18

Lisp

Lisp. A functional language. 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. Fundamentals.

Download Presentation

Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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!

More Related