Language
For basics of language use the awesome:
- Go BootCamp (more complete)
- Golangbot (more concise)
With Go you'll probably write a bit more code (boilerplate) than with many other languages. Go is a simple language and there might be missing some functions like shuffle the array and so on.
Reference vs Value types
Variadic function
LAMBDA CALCULUS
- I will be comparing with Javascript, because it is closer to lambda notation
Identity function
- lambda notation
λx.x (y)
- In Javascript:
(x => x)(3) // 3
- In Go
func(x int) int {return x}(3)
Constant function
- lambda notation
λx.y (z)
- In Javascript
let y = 3
(x => y)(1) // 3
- In Go
y := 3
func(x int) int { return y }(1) // 3
Lambda with many values
- Lambda can only have one argument per function
We will use "currying" - chaining functions with arity of 1
lambda notation
λx.λy.t
- In Javascript
var lambda = (x => y => x + y)
- In Golang
package main
import (
"fmt"
)
func mkAdd(a int) func(int) int {
return func(b int) int {
return a + b
}
}
func main() {
add2 := mkAdd(2)
add3 := mkAdd(3)
fmt.Println(add2(5), add3(6)) // 7 9
}
Reduction
- lambda notation
λx.λy.y x
λy.y[x:=x]
λy.y
x
- In Javascript
const first = (y => y)
const second = (x => first)
second(first) // [Function: first]
second(first)(3) // 3
- In Go
first := func(y int) int { return y }
second := func(x func(int) int) func(int) int { return x }
second(first)(3) // func(int) int
second(first(3) // 3