200 likes | 316 Views
ITEC 380. Organization of programming languages Lecture 6 – Functional Programming. Review. Lambda Loops Lazyness + Macros Structures / classes Homework Questions?. Book link. http://mitpress.mit.edu/sicp/full-text/book/ book.html More about basics of languages Optional for study.
E N D
ITEC 380 Organization of programming languages Lecture 6 – Functional Programming
Review • Lambda • Loops • Lazyness + Macros • Structures / classes • Homework • Questions?
Book link • http://mitpress.mit.edu/sicp/full-text/book/book.html • More about basics of languages • Optional for study
Objectives • Review basics of LISP • Debugging / Lower level implementation • Interfacing with the outside world • General discussion of wrapping • Exam next week (midterm)
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
FunctionsOperations • 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) (defunaddtwo () (* (- *big* *small*) 2 ) )
Local variables • Specify the scope of a problem (defunlocalTest() (let ((a 5) (b 6)) (c(+ a b)) ) )
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") )
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)
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
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 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/gears-raw.lisp#L17
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…
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-librarylibcurl (:unix (:or "libcurl.so.3" "libcurl.so")) (t (:default "libcurl"))) (use-foreign-library libcurl)
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)
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)))))
Investigate • How does Lisp work? Two functions show how • (disassemble ‘functionName) • Turn on trace • (trace functionName) • Run functionName and see what happens (defunfactorial (n) (if (plusp n) (* n (factorial (1- n))) 1))
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
Next week • Midterm exam • Prolog • Project 1