Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ITEC 380 Organization of programming languages Lecture 3 – Functional Programming."— Presentation transcript:

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

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

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

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

5 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 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo 8oooo `-__|__-' 8 8 8 8 8 | 8 o 8 8 o 8 8 ------+------ ooooo 8oooooo ooo8ooo ooooo 8 Welcome to GNU CLISP 2.49 (2010-07-07) Copyright (c) Bruno Haible, Michael Stoll 1992, 1993 Copyright (c) Bruno Haible, Marcus Daniels 1994-1997 Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998 Copyright (c) Bruno Haible, Sam Steingold 1999-2000 Copyright (c) Sam Steingold, Bruno Haible 2001-2010 http://www.clisp.org/

6 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

7 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

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

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

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

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

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

13 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

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

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

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

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

18 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

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

20 Grammar Next week Functional programming - Lisp


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

Similar presentations


Ads by Google