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

Slides:



Advertisements
Similar presentations
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Advertisements

ITEC 380 Organization of programming languages Lecture 4 – Functional Programming.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Guide To UNIX Using Linux Third Edition
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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
Yu-Tzu Lin ( 林育慈 )
Digital Electronics Data Structures LISP
MIPS coding. SPIM Some links can be found such as:
Multiple Returns and Extra Parameters “LISP is unique in its capability to return more than one value (or no value at all) from a form. This neatly avoids.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
F RANZ I NC. Optimizing and Debugging Programs in Allegro CL By Duane Rettig April, 2007.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
ITEC 380 Organization of programming languages Lecture 3 – Functional Programming.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
ITEC 380 Organization of programming languages Lecture 7 – Prolog.
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
PRACTICAL COMMON LISP Peter Seibel 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Miscellaneous Topics Packages –Provide separable name spaces for functions, constants, macros –We can encapsulate code in packages like classes in Java.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Control Structures CSC 358/ Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Section 15.4, 15.6 plus other materials
PROGRAMMING LANGUAGES
Variables, Environments and Closures
Getting Started with Lisp
Variables, Environments and Closures
FP Foundations, Scheme In Text: Chapter 14.
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Tonga Institute of Higher Education
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Common Lisp II.
More Scheme CS 331.
SPL – PS1 Introduction to C++.
Presentation transcript:

ITEC 380 Organization of programming languages Lecture 6 – Functional Programming

LISP Review Lambda Loops Lazyness + Macros Structures / classes Homework –Questions?

LISP Book link text/book/book.htmlhttp://mitpress.mit.edu/sicp/full- text/book/book.html More about basics of languages Optional for study

LISP Objectives Review basics of LISP Debugging / Lower level implementation Interfacing with the outside world General discussion of wrapping Exam next week (midterm)

LISP Global Variables Not good practice, but an example of variables in Lisp Example (defparameter *tiny* 1) (defparameter *large* 10) *tiny* *large* Note: * Is just a stylistic convention

LISP Functions Operations Create a function accomplish a task Example Note: Functions return their information by default (no need for return value) (defparameter *small* 1) (defparameter *big* 10) (defun addtwo () (* (- *big* *small*) 2 )

LISP Local variables Specify the scope of a problem (defun localTest() (let ((a 5) (b 6)) (c(+ a b)) )

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

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

LISP 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

LISP In-class coding Problems –Given a list of items as a parameter to your function, return true if there are any duplicates, nil other wise

LISP Lisp beyond the shell OpenGL - Common graphical language C based, how do we write Lisp w/ it? Example – opengl/blob/master/examples/mesademos/ge ars-raw.lisp#L17

LISP Bindings Many different approaches we will look at 2 CFFI –Common Foreign Function Interface –I.e. how to interface with the outside world Situation –Have X project you want to use in your code –You speak Greek they speak French…

LISP Example From Common-lisp.net (asdf:oos 'asdf:load-op :cffi) ;;; Nothing special about the "CFFI-USER" package. We're just ;;; using it as a substitute for your own CL package. (defpackage :cffi-user (:use :common-lisp :cffi)) (in-package :cffi-user) (define-foreign-library libcurl (:unix (:or "libcurl.so.3" "libcurl.so")) (t (:default "libcurl"))) (use-foreign-library libcurl)

LISP Example Using variables (defctype curl-code :int) ;;; Initialize libcurl with FLAGS. (defcfun "curl_global_init" curl-code (flags :long)) (defctype easy-handle :pointer) (foreign-funcall "curl_easy_setopt" :pointer *easy-handle* curl-option :nosignal :long 1 curl-code)

LISP Connection to the machine How does Lisp work? C Program or –From stack overflow (defx86lapmacro %car (src dest) (target-arch-case (:x8632 `(movl x8632::cons.car (%,src)) (%,dest))) (:x8664 `(movq x8664::cons.car (%,src)) (%,dest)))))

LISP Investigate How does Lisp work? Two functions show how (disassemble ‘functionName) Turn on trace –(trace functionName) –Run functionName and see what happens (defun factorial (n) (if (plusp n) (* n (factorial (1- n))) 1))

LISP Others SWIG –C/C++ Program that allows for multiple languages to execute said code Example –VR Toolkit –Scripting to work with small parts of the project

LISP Next week Midterm exam Prolog Project 1