Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

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

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

5 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

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

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

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

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

10 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

11 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

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

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

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

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

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

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

18 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

19 LISP Next week Midterm exam Prolog Project 1


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

Similar presentations


Ads by Google