1 / 19

ITEC 380

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.

vernon
Download Presentation

ITEC 380

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Review • Lambda • Loops • Lazyness + Macros • Structures / classes • Homework • Questions?

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

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

  5. 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. 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 ) )

  7. Local variables • Specify the scope of a problem (defunlocalTest() (let ((a 5) (b 6)) (c(+ a b)) ) )

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

  13. 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. 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)

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

  18. 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. Next week • Midterm exam • Prolog • Project 1

More Related