Model -> View -> Message
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
-- MODEL(State)
type alias Model = {value: Int}
initModel : Model
initModel = { value = 0 }
-- UPDATE
type Msg = Increment | Decrement
update msg model = 
	case expression of 
		Increment ->
		| {model | value = model.value + 1}
		Decrement ->
		| {model | value = model.value - 1}
-- VIEW
view model =
| div [] [
| button [onClick Decrement] [text "-"]
| div [] [text (String.fromInt model.value)]
| button [onClick Increment] [text "+"]
| ]Browser sends a message to the Elm runtime
Elm runtime calls Update which sends back a Model
- Speed over other javascript frameworks like Angular and React
Array-based language
- operators are called verbs
- a line is called a phrase
- variables are called nouns
4*3+2
This returns 20 because addition is done before multiplication. You can use parenthesis for normal order of operations.
i is the index operator that lets you initialize a vector.
name =. i.5This creates a vector of 0 1 2 3 4
Every function has a monadic and diatic definition.
The creators of J made something a website called NuVoc which lists out all of the different operators. It can be helpful since in J, operators like +. and +: do very different things.
The insert operator / is an adverb that applies a verb to every item in a list.
- high-performance
- expressive
- meta-programming
- $is a string interpolation operator
$(1+2)- dynamic with optional types
A function is compiled when it is called
test = "Hello World"A = [1,2,3,4,5]Arrays indexes start at 1
You can get the last element of an array using end
A[end]Arrays can have elements of different types.
[1.5, 1, "foo"]Example: adding a floating-point number and an integer makes performance slow
function sumofsins2(n::Integer)
	r = 0.0
	for i in 1:n
		r += sin(3.4)
	end
	return r
endUsing two floating-point numbers improves performance
function sumofsins2(n::Integer)
	r = 0.0
	for i in 1:n
		r += sin(3.4)
	end
	return r
end
macro welcome(name)
	return :(println("hello ", $name, " like CS-3314"))
endCall the macro like so:
@welcome("foo")squareall(A) = map(x->x^2, A)- Create for teaching kids how to program
- Block-based visual programming language
- Created at MIT, led by Mitchel Resnick
- 40 million projects and 40 million monthly users
- You can share code on the website and anyone can remix it
Makes kids less frustrated because there are no syntax errors. You just can connect some blocks together.
A block is what we would call a function
You can use the ask block to get user input
You can use the say block to print
Scratch can detect lots of events like loudness which detects how loud the sound from your computer mic is
We can define our own types like so
str_sequence = List[str]We can constrain parameter types like so
def flip_seq_static(collection: str_sequence)def print_people(collection: PersonCollection) -> NoneLet's create a int stack class
class IntStack:
		def __init__(self) -> None:
			self.data: List[int] = []
		def push(self, elem:int) -> None: 
			self.data.append(elem)
		def pop(self) -> int:
			if len(self.data): return self.data.pop()
		def top(self) -> int:
			if len(self.data): return self.data[-1]What if we wanted a stack that worked with many types?
T = TypeVar('T')
class GenStack(Generic[T]):
	def __init__(self) -> None:
			self.data: List[T] = []
		def push(self, elem:T) -> None: 
			self.data.append(elem)
		def pop(self) -> T:
			if len(self.data): return self.data.pop()
		def top(self) -> T:
			if len(self.data): return self.data[-1]#school/f19/proglang-f19