Transcript
Page 1: Programming Languages: Prolog, Clojure, F#

PROGRAMMING LANGUAGES:PROLOG, CLOJURE, F#Jared Wheeler

Page 2: Programming Languages: Prolog, Clojure, F#

Prolog 1972 Development Declarative Language No Algorithms

Define the Problem It finds the answer

Page 3: Programming Languages: Prolog, Clojure, F#

Parts Knowledge Base

Facts Basic Assertions

Rules Inferences about Facts

Queries

Page 4: Programming Languages: Prolog, Clojure, F#

Naming constant Variable or _variable List – [1, 2, 3] Tuple – (1, 2, 3)

Fixed Length

Page 5: Programming Languages: Prolog, Clojure, F#

Unification No Assignments Prolog tries to make variables and

constants equal If succeeds, an inference can be made

Page 6: Programming Languages: Prolog, Clojure, F#

Results Yes No

Answers unasked question of if execution completed

Page 7: Programming Languages: Prolog, Clojure, F#

Recursion Call Rule from within Rule Tail-Recursion Optimization

Put Recursive call at End Prolog keeps memory use steady

Page 8: Programming Languages: Prolog, Clojure, F#

Uses Solving Systems within Constraints

Puzzles Natural Language Processing AI

Page 9: Programming Languages: Prolog, Clojure, F#

Weaknesses Utility Scaling to Large Data Sets

Depth First Search for Answers Learning Curve

Page 10: Programming Languages: Prolog, Clojure, F#

Prolog Demo Knowledge Base File Execution

Page 11: Programming Languages: Prolog, Clojure, F#

Clojure Lisp for JVM

Scheme, lisp-1, not Common Lisp, lisp-2 Difference in namespacing functions and

data Data As Code

Page 12: Programming Languages: Prolog, Clojure, F#

Variables (def variableName item)

Prefix notation (let value variableBound)

Temporary binding of value to a variable

Page 13: Programming Languages: Prolog, Clojure, F#

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

Page 14: Programming Languages: Prolog, Clojure, F#

Functions (defn name [parameters] body)

Parameters within a vector (fn [parameters] body)

Anonymous function

Page 15: Programming Languages: Prolog, Clojure, F#

Recursion No Tail-Recursion Optimization

Working around JVM too much (loop [parameter1 boundVariable, …]

(recur (parameter1In)))

Page 16: Programming Languages: Prolog, Clojure, F#

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

Page 17: Programming Languages: Prolog, Clojure, F#

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

Page 18: Programming Languages: Prolog, Clojure, F#

Concurrency Software Transactional Memory

Inspired by database transactions (def reference (ref “some data”))

@reference gives “some data” Cannot change without a dosync function

Page 19: Programming Languages: Prolog, Clojure, F#

Interfaces defprotocol

Define a contract defrecord

Implements the protocol Records are immutable

Page 20: Programming Languages: Prolog, Clojure, F#

Macros Two stages of Clojure Execution

Macro Expansion Execution

defmacro to Create

Page 21: Programming Languages: Prolog, Clojure, F#

Strengths Powerful and Flexible Use of JVM Lisp minus parenthesis and reader

macros Concurrency Lazy Evaluations

Page 22: Programming Languages: Prolog, Clojure, F#

Weaknesses Complex Prefix Notation Readability Complex Recursion Optimization

Compared to other Functional Languages

Page 23: Programming Languages: Prolog, Clojure, F#

Clojure Demo General Form

Page 24: Programming Languages: Prolog, Clojure, F#

F# Built by Microsoft Research Runs on the .NET Framework

Page 25: Programming Languages: Prolog, Clojure, F#

Variables Assignments by Let statements

Immutable unless ‘modifiable’ attribute at declaration

Statically Typed Strong Type Inferencing

Page 26: Programming Languages: Prolog, Clojure, F#

Functions Let functionName Parameters = body Whitespace-sensitive Parameters have type inferencing

Sometimes require (variableName:type) Functions can be Passed as Parameters

Page 27: Programming Languages: Prolog, Clojure, F#

Anonymous Functions Lambda Expressions

(fun params -> body)

Page 28: Programming Languages: Prolog, Clojure, F#

Lists [ 1; 2; 3] = [1..4]

Ranges allowed List.map (function) listName

Applies a function to all members List.filter (function) listName

Returns members that the function returns true for

Page 29: Programming Languages: Prolog, Clojure, F#

Array [| 1; 2; 3|]

Page 30: Programming Languages: Prolog, Clojure, F#

Record Grouped Data with Member Names

Immutable Defines data types

Keyword type to define Mutate using with Members can be optional

Page 31: Programming Languages: Prolog, Clojure, F#

Discriminating Joins EnumerationType object = | Option1| Option2

Page 32: Programming Languages: Prolog, Clojure, F#

Forward Pipe OperatorList.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

Page 33: Programming Languages: Prolog, Clojure, F#

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

Page 34: Programming Languages: Prolog, Clojure, F#

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

Page 35: Programming Languages: Prolog, Clojure, F#

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

Page 36: Programming Languages: Prolog, Clojure, F#

F# Demo General Form Record Creation Record Mutation


Top Related