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.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Stacks Chapter 11.
Prefix, Postfix, Infix Notation
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
Infix, Postfix, Prefix.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Functional Programming in Scheme
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Linear Data Structures LIFO – Polish notation Context Saving.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Functional Programming: Lisp MacLennan Chapter 10.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
CS314 – Section 5 Recitation 10
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Functional Programming Languages
COMPSCI 107 Computer Science Fundamentals
MEMORY REPRESENTATION OF STACKS
ASTs, Grammars, Parsing, Tree traversals
(Functional Programming) Reference: R.Sebesta, Chapter 15
CSC 172 DATA STRUCTURES.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Infix to Postfix Conversion
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Computer Science 2 5/17/2016 Finish the Queue Program
Stacks.
Topic 15 Implementing and Using Stacks
Functional Programming: Lisp
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

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 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

A Lisp List ( A B C D) bounded by parenthesesseparated by spaces

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

Basic Operations use PREFIX notation operator precedes operands ( ) Abs(x) operator operand

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

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

Prefix notation simplifies (– + / * 2 2 ) In lisp In standard algebra ( 10 / – 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.

Prefix notation simplifies (– + / * 2 2 ) – ( + / * 2 2 ) + – ( / * 2 2 ) / + – ( * 2 2 ) 10 / + – (5 7 * 2 2 ) 5 10 / + – 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!

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

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

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))

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

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

Iteration Use recursion Before seeing recursion –Learn how to define a function –Learn how to define a recursive function

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!

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