ECEN 5623 RT Embedded Systems
Total Page:16
File Type:pdf, Size:1020Kb
CS 332 Programming Language Concepts Lecture 11 – Alternative Programming Languages (Functional – LISP Declarative Logical - PROLOG) March 17, 2020 Sam Siewert Reminders Exercise #4 - Your PL study proposals!! - Pair Up if you Like – Williams, Laurie, et al. "Strengthening the case for pair programming." IEEE software 17.4 (2000): 19-25. [Canvas] – 4 Objective Measure, 4 Subjective vs. 3 Objective, 3 Subjective Exercise #5 - work on project, firm up your proposal Exercise #6 - is your Final Report Exam #2, Last Week of Class, Review Before Final Exam – Your Presentation – Team or Individual Analysis or Mini-Alternate Programming Language – Submit report and slides – Make video for slides or walk-through them with me on Skype Sam Siewert 2 Extra Credit – Updated Dijkstra Flag Problem Correct spelling of Dijkstra! (please) Dutch, French, and Czech flag require sorting of equal numbers of Red, Blue and White pixels in a PPM (flags) See Assignment #3 for Extra Credit - Turn in Anytime ¾ R, W 3/8 Red 3/8 White ¼ Blue By inspection Sam Siewert 3 Quick Note on Lab #4, Part 3 Hybrid Concept What I asked for is Not Natural! Sort, Add/Delete Like a Linked List, Dump Like Memory Encapsulate Linked-List and Array in a Single Object and/or Instantiate with Either? How Does One Do this? OO? Many Options, Here’s a Few, Take an Approach 1. One Class with Methods the Encapsulates Both An Array Hybrid Prototype - Outcome and Linked List (to provide both behaviors) 2. Make your Linked-List Object Work like an Array (Best Done with C++ where you can Overload “[]”) and Use Methods In Place of Array 3. Hybrid Data Structure – Array with Linked List of Pointers to It! – So Called Write-Anywhere Scheme with Data Map 4. …? http://www.popsci.com/article/cars/will-helicopter-truck-fly Sam Siewert 4 Quick Note: Research vs. Development Vision, Objectives, Goals NSF (OMB) – Acquisition of Knowledge "Research" is defined as a systematic study – Requirements Less Emphasized, To Be directed toward fuller scientific knowledge or Determined understanding of the subject studied. Research is classified as either basic or applied, according – Proof-of-Concept and Prototypes to the objectives of the investigator. Emphasized – Repeatability of Results, Both Expected "Development" is the systematic use of and Unexpected knowledge and understanding gained from research directed toward the production of useful materials, devices, systems, or Experimental Research methods, including design and development – Emphasis on Validation & Verification of prototypes and processes. – Design of Experiments (PL Compare) DoD – Data Analysis Theoretical Research – Models – Mathematical, Logical, Statistical, Probability, Simulation – Analysis of Problems and Domains Sam Siewert https://www.rand.org/content/dam/rand/pubs/monograph_reports/MR1194/MR1194.appb.pdf 5 Simple View of Research [R&D] Acquisition of Knowledge – Theory or Experiment Defined by Existing Knowledge and Expectations E.g., What Alternate Programming Language might fit my Project? Development Innovation / Adv. Tech Applied Basic • Well-known (standard) • Recombination of well-known • Expectations • Nobody knows • Specified application • Specified application • Misconceptions • No application • Assumptions • Specific Problem or Class Spectrum Sam Siewert 6 Functional PL Concepts Based on Lambda Calculus Output is Math Function of Inputs with No Internal State, No Side Effects – Lambda Calculus Also Used to Specify PL Semantics (Denotational Semantics) – Nested Functions and Recursion – Everything is a Function, Including Data (Much like Everything is and Object in OOP) Common Lisp – Symbolic AI, Functional Core – Online C-Lisp Book/Manual – Syntax Overview Haskell – Purely Functional Sam Siewert 7 LISP has a Long History Introduced in 1955 (longest lived PL along with FORTRAN) Many dialects and embedded use in tools Examples: LISP in Emacs LISP in AutoCAD https://en.wikipedia.org/wiki/Lisp_(programming_language) Sam Siewert 8 Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most Widely Used Numerous Lisp Variants – E.g. Scheme – sudo apt-get install scm – Good so you can follow along in Chapter 10, PLP On PRClab, Use Gambit Scheme Interpreter Sam Siewert 9 Lisp Basics – Winston & Horn (setf friends '(dick jane sally)) t Tested in friends nil C-Lisp (setf enemies '(troll grinch ghost)) pi (setf enemies (remove 'ghost enemies)) (append alist blist) enemies (length alist) (setf friends (cons 'ghost friends)) (reverse alist) friends (setf sam '((height 6.1) (defun newfriend (name) (weight 210))) (setf enemies (remove name enemies)) (assoc 'height sam) (setf friends (cons name friends))) (setf nlist '(1 2 3 4 5 6 7 120)) (first '(a b c)) (max 1 2 3 4 5 6 122) (rest '(a b c)) ; parallel let (rest '(c)) (setf x 'outside) (first (rest '(a b c))) (let ((x 'inside) (first '(rest (a b c))) (y x)) (car '(a b c)) (list x y)) (cdr '(a b c)) ; sequential let (cadr '(a b c)) (setf x 'outside) (second '(a b c)) (let* ((x 'inside) (setf alist '(1 2 3 4 5 6)) (y x)) (setf alist '(a b c d e) (list x y)) blist '(x y z)) () (expt 2 3) Sam Siewert (cos 3) 10 Modern C-Lisp IDE LispWorks - Download Graphical Debugger Interactive Windows Mac Linux Sam Siewert 11 Everything is Really a List in Lisp http://www.gigamonkeys.com/book/they-called-it-lisp-for- a-reason-list-processing.html Lists always Have Parens around them To get to an Atom, Use Car on List of ONE Sam Siewert 12 CSL Lisp REDUCE Algebraic Simplification, Derivation, Etc. Simplifying Equations Compared to Numerical Solutions Sam Siewert 13 Lisp Applications and Activity DS-1, Deep-Space 1 – RAX – Remote Agent – Flew Common Lisp Interpreter (Lisp Works) ROS Lisp - http://wiki.ros.org/roslisp Planet Lisp - http://planet.lisp.org/ MIT course – Lisp Intro to Programming https://common-lisp.net/ http://norvig.com/paip.html Sam Siewert 14 Brief Lisp History Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT). McCarthy published its design in a paper in Communications of the ACM in 1960, entitled "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I".[9] He showed that with a few simple operators and a notation for functions, one can build a Turing- John McCarthy complete language for algorithms. Sam Siewert 15 Other Uses of LISP – “Latin Roots for Modern Multi-paradigm and Functional” Automated Theorem Proving Game Theory and AI Games Natural Language Processing Automated Planning and Scheduling Optimization Searches Semantic Web and Search Largely Being Replaced by Functional Features in Python, C++, C# and Newer Lisp Dialects such as Scheme and Racket as well as multi-paradigm Caml and Haskell, Swift https://chessprogramming.wikispaces.com/Herbert+Simon Sam Siewert 16 What is Status of Lisp Lisp Python new/current AI teaching programming Pascal language Scheme ML Python is often used for Intro to Programming with Java Ada Smalltalk C++, Java, Swift, Python, C#, Common Lisp etc. are used for production Miranda C++ AI products CLOS Caml Eiffel Haskell Lisp is a great way to learn functional programming Python OCaml based on simplicity, long Ruby history, and pure functional C# subset F# Sam Siewert 17 Shallow to Deep – Structural or Mathematical Equivalence Testing Equality and Equivalence Gets Interesting (Equality Predicates) – Structurally the Same – String Comparison – Real Valued Comparison – Same Function, Same Evaluation of a Function … Most PLs Have Simple Evaluation and Compare Sam Siewert 18 Some Examples … Using A Simple List of Numbers … Most Imperative Procedural PLs have Shallow Copy (I Can’t Assign a Struct to a Struct, Array to an Array, List to a List, etc., Just Single Values) Likewise Comparison is Typically Numerical Only Lambda expressions Two lists … Sam Siewert 19 Note on ANSI Common LISP Steel Bank Common LISP is ANSI Strict Requires Definition Prior to Elaboration (ANSI Addition) Many LISP Interpreters Simply Create the Definition Automatically (E.g. CLISP) Result is the SAME, just WARNING CLISP Has No Warning Sam Siewert 20 Digging Deeper … Typically Lisp is Taught in Introduction to AI (Symbolic) Python often used instead of Lisp Today http://people.csail.mit.edu/phw/Books/#Lisp CLISP - https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/clm.html Often a First Year Graduate or Senior Year Undergraduate Computer Science Class Patrick H. Winston - Books Sam Siewert 21 Lisp Predicates and Functions ; Conditional expression (defun prob-term (p) (cond ((> p 0.75) 'very-likely) ((> p 0.5) 'likely) ((> p 0.25) 'unlikely) (t 'very-unlikely))) ; Predicates and conditionals (equal '(1 2 3 5 4 7 9 11) '(1 2 3 5 4 7 9 11)) (equal alist blist) (member 'e alist) (equal (lambda (x) (* x x)) (lambda (x) (* x x))) (eql (lambda (x) (* x x)) (lambda (x) (* x x))) (equal (lambda (x) (* x x)) (lambda (y) (* y y))) (= 4 4.0) (defun square (n) (* n n)) (mapcar #'square '(1 2 3)) (mapcar #'= '(1 2 3) '(3 2 1)) (funcall #'(lambda (param) (first param)) '(a b c d e f)) (setf trees '((maple shade) (apple fruit))) (member '(maple shade) trees) (member '(maple shade) trees :test #'equal) Sam Siewert 22 Lisp Recursion Designed for Recursion, Nesting and Mapping Functions to Data of Most any Type ; Recursion (defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2))))) (defun count-elements (list) (if (endp list) 0 (+ 1 (count-elements (rest list))))) (trace count-elements)