Download presentation
Presentation is loading. Please wait.
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 parenthesesseparated by spaces
5
A Lisp List ( A B C D) Lists are composed of atoms OR sublists ( (A) B (C D))
6
Basic Operations use PREFIX notation operator precedes operands ( + 2 4 ) Abs(x) operator operand
7
Other simple examples ( + 2 4 ) ( - 2 4 ) ( + 2 – 7 4 ) 6 -2 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 (– + / 10 5 7 * 2 2 ) In lisp In standard algebra ( 10 / 5 + 7 – 2 * 2 ) Need to know precedence rules 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 5 7 * 2 2 ) 10 / + – (5 7 * 2 2 ) 5 10 / + – 2 + – 7 2 + – (7 * 2 2 ) 9 – (* 2 2 ) * 9 – (2 2 ) 2 * 9 – (2)(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 (car ( (A) B (C D))) = (A) ( cdr ( A B C D)) = ( B C D) (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 if (a > b) return 1 else if ( c > d) return 2 else if ( e > f) return 3 else return 4 ( cond ( ( > a b ) 1) ( ( > c d ) 2 ) ( ( > e f ) 3 ) ( ELSE 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 ( Add x y ) ( + x y ) ) Add (x, y) { return x + y; } In lisp Works for two simple atoms, but what about lists? You need recursion!
18
Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) int Addup (a,n) { sum=0; for (i=0, i<n; i++) sum+= a[i]; return sum; } in lisp Summing elements in a list(table) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work! int Addup (a,n) { sum=0; if (n==1) return a[0]; else return a[0]+Addup(a[1],n-1) } Recursively in c
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.