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