Simple Introduction to Clojure with Analysis

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 1 μήνα)

69 εμφανίσεις

Simple Introduction to Clojure with
Analysis


By: Shannon
Birchell


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+)


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



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



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”







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)))




Java integration


(
javax.swing.JOptionPane
/
showMessageDialog

nil
(
str

"Hello Everyone"))



Is equivalent to java code:

javax.swing.JOptionPane.showMessageDialog
(null,
"Hello
Everyone“);


Dot operator “.” performs java
functinality

(. String split “,”)
-
>
String.split
(“,”);


Most java objects do not work in the dynamic
environment







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










Overview and analysis of R


By Shannon
Birchell


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





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




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


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





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” )



Creating a matrix


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


0 1


Creating a function


Default values as parameters

x <
-

function(a , b = 1 ){ return( a + b );}

x(1,2) is 3

x(4) is 5


Creating a graph


Plot, box plot, density… etc


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


Micheal

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



The R Project,
www.r
-
project.org



Simple Introduction to Prolog with Analysis


By: Shannon
Birchell



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




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


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 relationships

Given the previous example “There is a dog called red”


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

Therefore called(x):= name(x), animal(x).



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



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