simple introduction to clojure with analysis by: shannon birchell

26
Simple Introduction to Clojure with Analysis By: Shannon Birchell

Upload: meagan-jefferson

Post on 20-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Simple Introduction to Clojure with Analysis

By: Shannon Birchell

Page 2: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Foundation

First Class Functions Inherits concepts from Lisp Every expression is a function Functions attempt to be pure (not possible) Concurrency is very stable

Software Transaction Memory(STM)

Uses JVM Hybrid Dynamically run code

REPL Inherits all Java classes (4k+)

Page 3: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Types

Primitives Not so interesting:Number, String, Boolean and char Coercion takes place on the JVM in byte code

Interesting Lisp primitives List: Linked List without element removal

capacities Vector: like linked list but more efficient (less

capabilities) Keyword: used for Map as key Map: equivalent to Hashmap Set: same as mathematics

Page 4: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Variables

Dynamic type binding Variable Scope

Stack Dynamic Explicitly stated global scopes

Value bindings are immutable Values cannot be changed Mapping are used to transform variables Enhanced stability and reliability

Page 5: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Functions

Function Declaration Anonymous function

(fn [<parameters>]( <element> <element> … )) Explicit function(defn <name> [<parameters>] ( <element>

<element…)) Functions can be self documented using

Function closure A function and all its current values can be stored

as a variable. It is said, “The function is closed over”

Page 6: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Function Cont.

Some functions String: (str “hello” “world”) -> hello world (cons 4 ‘(1 2 3)) -> 1 2 3 4 (first ‘(1 3 5)) ->1 (doc someFunct) -> description

First class functions Parameters can be functions(* (+ 1 1) (+ 1 1)) = (* 2 (+ 1 1))=(* 2 2)=4

Return values can be functions (defn f1 [p1] (fn [p2] ( str p1 p2)))

Page 7: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Java integration

Java integration (javax.swing.JOptionPane/showMessageDialog

nil (str "Hello Everyone"))

Is equivalent to java code:javax.swing.JOptionPane.showMessageDialog(nu

ll, "Hello Everyone“); Dot operator “.” performs java functinality

(. String split “,”) -> String.split(“,”); Most java objects do not work in the

dynamic environment

Page 8: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Demonstration

Page 9: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Characteristics

Readability Orthogonal structures.(. String contains “hi”) -> String.contains(“hi”); iteration is done through recursion and is cryptic

Writeability Highly nested structures require substantial

forethought (if(< (+ 1 1) (3/4) “write”)) Highly nested structures unify code into cohesive

chunk. Cost

Different than main stream – learning curve Free software with good libraries

Page 10: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Overview and analysis of R

By Shannon Birchell

Page 11: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Foundation

Domain specific Statistical and numerical analysis Used as a scripting language Run commands real time in R environment

R Based on S syntax Created by Bell Laboratories in 1976 Alternative to Fortran Static scoped Imperative language

Class Oriented Like OO but uses predefined classes

Page 12: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Variables

Primitives Vectors logical, integer, double, complex, character, or

raw Coercion is used for different vectors of different size

<1,2> + <1,3,5,7> = <1+1,2+3,1+5,2+7>Logical Error checking reduced!!!

Type coercion is character, integer, double, logic Implicit type binding

Lists Behave like a vector without enforcing a type

Keys/Names can be associated with entries Use brackets with name string to accesses elements

Page 13: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Variables cont.

Matrix Is a vector that is mapped to the specified

dimensions Coercion done when dim doesn’t match

An associated array Indexes start with 1

Tables Matrix with elaborate row and column names Inherits additional methods: colnames &

rownames

Page 14: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Objects and functions

Objects are black box unlike Java

Objects are implicitly associated with a class Auto inherit mode and Attribute methods More that one class may be inherited

There are many built in functions Associated with classes but for all practical

purposes act completely like functions

Page 15: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Operations

Important Functions Function c() creates a vector Function list() creates a vector of generic types Function dim() maps vector to an array Function plot() graphs a dataset

Creating a objects “<-” assign to an object

Can assign values, functions or other objects Creating a vector: x <-c(1,2,3,4) Creating a list x <- list( “one”, 1, three=

”three” )

Page 16: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Expressions

Creating a matrix array( c(1,0,0,1), dim=c(2,2))-> a : 1 0 0 1

Creating a function Default values as parametersx <- function(a , b = 1 ){ return( a + b );}x(1,2) is 3x(4) is 5

Creating a graph Plot, box plot, density… etc

Page 17: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Demonstration

Page 18: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Characteristics

Readability Black box objects make it difficult to understand

what objects can do Familiar syntax

Writeability Large number of operators make full language use

difficult Large number of keywords is problematic Consistent use of expressions make tasks easy

Cost Syntax is familiar to C and C++ Software and packages are free

Page 19: Simple Introduction to Clojure with Analysis By: Shannon Birchell

References

Micheal J. Crawley, “The R Book” , Wiley, 2007

The R Project, www.r-project.org

Page 20: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Simple Introduction to Prolog with Analysis

By: Shannon Birchell

Page 21: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Foundation

Languages is based on first order logic

Queries perform complex analysis on truth statements Can display true of false at the simplest level Can display all values that are true

Page 22: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Data

Atom A string of characters that is tokenized as an

id Has no other purpose Scope is global

Numbers Floats and Integers only

Variables Starts with a capital letter Anonymous variable “_” Scope is rule

Page 23: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Rules

An Axiom is a statement of truth Immutable – always true“There is a dog call red”

There must be an animal dog : animal(dog). There must be a name red: name(red).

Rules are axioms with relationshipsGiven the previous example “There is a dog called

red” The relationship that bind “dog” and “red” the

verb calledTherefore called(x):= name(x), animal(x).

Page 24: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Unification & Backtracking

Unification is how prolog derives the truth of a query This is done by checking all axioms against

rules Object is to make the implication query equal:

axiom1, axiom2 axiomN….rule1, rule2…query ? Rule LHS <- rule RHS <- axioms

Backtracking is the process of performing unification over all possibilities

Page 25: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Demonstration

Page 26: Simple Introduction to Clojure with Analysis By: Shannon Birchell

Characteristics

Readability Difficult to understand the relationships

But this is the point!!! Writeability

Simple statements of fact Simple syntax

Reliability Bad query catastrophically fail Unintended consequences of statements

unrealized