programming languages: prolog, clojure, f# jared wheeler

36
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Upload: curtis-peters

Post on 13-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

PROGRAMMING LANGUAGES:PROLOG, CLOJURE, F#

Jared Wheeler

Page 2: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Prolog

1972 Development Declarative Language No Algorithms

Define the Problem It finds the answer

Page 3: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Parts

Knowledge Base Facts

Basic Assertions Rules

Inferences about Facts

Queries

Page 4: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Naming

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

Fixed Length

Page 5: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Results

Yes No

Answers unasked question of if execution completed

Page 7: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Uses

Solving Systems within Constraints Puzzles Natural Language Processing AI

Page 9: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Weaknesses

Utility Scaling to Large Data Sets

Depth First Search for Answers Learning Curve

Page 10: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Prolog Demo

Knowledge Base File Execution

Page 11: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Variables

(def variableName item) Prefix notation

(let value variableBound) Temporary binding of value to a variable

Page 13: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Functions

(defn name [parameters] body) Parameters within a vector

(fn [parameters] body) Anonymous function

Page 15: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Recursion

No Tail-Recursion Optimization Working around JVM too much (loop [parameter1 boundVariable, …]

(recur (parameter1In)))

Page 16: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

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# Jared Wheeler

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# Jared Wheeler

Interfaces

defprotocol Define a contract

defrecord Implements the protocol

Records are immutable

Page 20: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Macros

Two stages of Clojure Execution Macro Expansion Execution

defmacro to Create

Page 21: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Strengths

Powerful and Flexible Use of JVM Lisp minus parenthesis and reader

macros Concurrency Lazy Evaluations

Page 22: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Weaknesses

Complex Prefix Notation Readability Complex Recursion Optimization

Compared to other Functional Languages

Page 23: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Clojure Demo

General Form

Page 24: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

F#

Built by Microsoft Research Runs on the .NET Framework

Page 25: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

Variables

Assignments by Let statements Immutable unless ‘modifiable’ attribute at

declaration Statically Typed

Strong Type Inferencing

Page 26: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Anonymous Functions

Lambda Expressions (fun params -> body)

Page 28: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Array

[| 1; 2; 3|]

Page 30: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

Discriminating Joins

EnumerationType object = | Option1| Option2

Page 32: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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

Page 33: PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler

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# Jared Wheeler

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# Jared Wheeler

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# Jared Wheeler

F# Demo

General Form Record Creation Record Mutation