ITEC 380 Organization of programming languages Lecture 3 – Functional Programming.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
ITEC 380 Organization of programming languages Lecture 4 – Functional Programming.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Slide 1 CS3 Fall 2005 Lecture week 13: Introduction to the Big Project.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ITEC 380 Organization of programming languages Lecture 6 – Functional Programming.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Basic Introduction to Lisp
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
Getting Started with Lisp
LISP LISt Processing.
LISP A brief overview.
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Presentation transcript:

ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar Review Grammars BNF/EBNF How to write a parser, why to do so Parse trees Types of parsers

Grammar Objectives Intro to lisp Getting an interpreter working Loading programs Basic lists / operations Combining / Parsing lists

Grammar Lisp Different evolutionary path than imperative / OO type programming –Much more math focused Removes the focus on the state of a program from the equation Works on lists of numbers / strings Case insensitive language!!!

Grammar Getting it running Visit Once you run it, you get something like i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I o 8 8 I \ `+' / I \ `-+-' / ooooo 8oooo `-__|__-' | 8 o 8 8 o ooooo 8oooooo ooo8ooo ooooo 8 Welcome to GNU CLISP 2.49 ( ) Copyright (c) Bruno Haible, Michael Stoll 1992, 1993 Copyright (c) Bruno Haible, Marcus Daniels Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998 Copyright (c) Bruno Haible, Sam Steingold Copyright (c) Sam Steingold, Bruno Haible

Grammar Ways to use Can pipe a program into it –clisp < filename.txt Can type code straight into the shell –(+ 3 2) Can load code from files –(load “filename.txt”) –Note: define functions in file, run by hand

Grammar Step 1 Start with the most evil part of any programming language: Global variables Example (defparameter *tiny* 1) (defparameter *large* 10) *tiny* *large* Note: * Is just a stylistic convention

Grammar Step 2 Create a function accomplish a task Example What does this print out? Does it matter where the )’s are (defparameter *small* 1) (defparameter *big* 10) (defun addtwo () (* (- *big* *small*) 2 )

Grammar Local parameters Let = freedom What are the advantages of local versus global? Can create local functions with flet Can allow recursion with local functions with labels (defun localTest() (let ((a 5) (b 6)) (c(+ a b)) )

Grammar Other ways setq –(setq Name ‘Me) list –(list Name ‘You and a dog named boo)

Grammar Data Numbers –4/6 => How would this be put into lisp? –4.0/6 => Ditto? Strings –Enclose in “ “ otherwise will be treated like a function Items as a list –‘(+ 2 1) versus (+ 2 1)

Grammar I/O Input data to a variable Output a variable (let ((A "Hello")) (princ A)) (let ((A (read))) (princ A))

Grammar Working with lists Three basic functions cons –Combine lists together car –Access the first item in a list cdr –Return the second element to the end of a list

Grammar Flexibility Can combine multiple car and cdrs together Can also cheat by using –First, second, third, fourth, etc… cadr cddr cadar Apple Peach Mango Pear Strawberry Blueberry Pineapple (Apple (Red Yellow Pink Lady) Peach (Georgia California) )

Grammar Inside lists How to check if a number is in a list –(member 5 ‘(3 4 5)) How access a particular item in the list –(find-if #’oddp ‘(1 2 4))

Grammar Conditionals Simple to use (if (conditional) (then) (else) ) Sometimes you will make a function call instead of the ‘(list data) Also have when and unless (if (> 5 3) (princ "Larger") (princ "Smaller") )

Grammar Conditionals Can have multiple conditionals Example (cond ((or (> 3 5) (>2 6) ) (princ “One”)) ((< 2 4) (princ “Two”)) )

Grammar Functions What is out there? –abs,sin, cos, tan, mod, round, min, max, expt, sqrt –first,rest,last, length –cons, append, list Type checking –listp,numberp,integerp,stringp, evenp, oddp Others –null, equal/eql/eq, and, or, not –Use eq for symbols, equal for all others

Grammar Missing? Given what you know of existing programming languages, what is currently missing? How do we work around these missing features?

Grammar Next week Functional programming - Lisp