PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler Prolog 1972 Development Declarative Language No Algorithms Define the Problem It finds the answer Parts Knowledge Base Facts Basic Assertions Rules Inferences Queries about Facts Naming constant Variable or _variable List – [1, 2, 3] Tuple – (1, 2, 3) Fixed Length Unification No Assignments Prolog tries to make variables and constants equal If succeeds, an inference can be made Results Yes No Answers unasked question of if execution completed Recursion Call Rule from within Rule Tail-Recursion Optimization Put Recursive call at End Prolog keeps memory use steady Uses Solving Systems within Constraints Puzzles Natural AI Language Processing Weaknesses Utility Scaling to Large Data Sets Depth First Search for Answers Learning Curve Prolog Demo Knowledge Base File Execution Clojure Lisp for JVM Scheme, lisp-1, not Common Lisp, lisp-2 Difference in namespacing functions and data Data As Code Variables (def variableName item) Prefix notation (let value variableBound) Temporary binding of value to a variable Data Structures List – Ordered Collection () Hold Code First element is always executed as a function Vector – Ordered Collection [] Hold Data Map – Key-value Pairs {} Clojure allows comma delimitations Keys can be used as functions to retrieve the value Set – Unordered Collection #{} Can be used as a function to find if an item is included Functions (defn name [parameters] body) Parameters within a vector (fn [parameters] body) Anonymous function Recursion No Tail-Recursion Optimization Working around JVM too much (loop [parameter1 boundVariable, …] (recur (parameter1In))) Sequences Common Parent to Most Data Structures Comes with many default functions every?, some?, filter, map, reduce, sort (for [x seq] function) Binds each member of the seq to x one at a time Lazy Evaluation (repeat 1) Repeats 1 until the process is killed (take 5 (repeat 1)) Repeats 1 five times Lazy Evaluation means infinite sets can be computed Evaluations only occur once needed Concurrency Software Transactional Memory Inspired by database transactions (def reference (ref “some data”)) @reference gives “some data” Cannot change without a dosync function Interfaces defprotocol Define a contract defrecord Implements the protocol Records are immutable Macros Two stages of Clojure Execution Macro Expansion Execution defmacro to Create Strengths Powerful and Flexible Use of JVM Lisp minus parenthesis and reader macros Concurrency Lazy Evaluations Weaknesses Complex Prefix Notation Readability Complex Recursion Optimization Compared to other Functional Languages Clojure Demo General Form F# Built by Microsoft Research Runs on the .NET Framework Variables Assignments by Let statements Immutable unless ‘modifiable’ attribute at declaration Statically Typed Strong Type Inferencing Functions Let functionName Parameters = body Whitespace-sensitive Parameters have type inferencing Sometimes require (variableName:type) Functions can be Passed as Parameters Anonymous Functions Lambda Expressions (fun params -> body) Lists [ 1; 2; 3] = [1..4] Ranges List.map (function) listName Applies allowed a function to all members List.filter (function) listName Returns members that the function returns true for Array [| 1; 2; 3|] Record Grouped Data with Member Names Immutable Defines data types Keyword type to define Mutate using with Members can be optional Discriminating Joins Enumeration Type object = | Option1 | Option2 Forward Pipe Operator List.sum (List.map (fun x -> x * 2) (List.filter (fun x -> x % 2 = 0) [0..100])) Into this: [0..100] |> List.filter (fun x -> x % 2 = 0) |> List.map (fun x -> x * 2) |> List.sum Currying Functions Have One Parameter Return a function asking for the next Repeats until all parameters are accounted for Parameter order matters Allows for New Operators Easier Also allows for different form of overloading Operators Overload Only a Select set of Operators Otherwise overwrites Unless done within a type Define new Operator For One Parameter Works due to currying Sublanguages Active Patterns Pattern Matching Customizable Allows labels within a pattern to call a function Quotations <@ … @> Stores code fragments Type Checks, but no execution F# 3.0 has Type Provider that can execute Together, they allow Sublanguages or Cross-Compilation F# Demo General Form Record Creation Record Mutation