Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save svanellewee/fffff13d96467bae3452 to your computer and use it in GitHub Desktop.

Select an option

Save svanellewee/fffff13d96467bae3452 to your computer and use it in GitHub Desktop.
Church Lambda; LISP in Go
package main
import "fmt"
type Any interface{}
type FuncType func(Any, Any) Any
type ConsType func(FuncType) Any
func Cons(a, b Any) ConsType {
return func(fn FuncType) Any {
return fn(a, b)
}
}
func Car(cons ConsType) Any {
head := func(a, b Any) Any { return a }
return cons(head)
}
func Cdr(cons ConsType) Any {
tail := func(a, b Any) Any { return b }
return cons(tail)
}
/*
foldl :: (a -> b -> a) -> a -> [b] -> a
foldl f z [] = z
foldl f z (x:xs) = foldl f (f z x) xs
*/
func foldl(fn FuncType, init Any, list ConsType) Any {
if list == nil {
return init
}
head,tail := Car(list),Cdr(list)
rest,_ := tail.(ConsType)
return foldl(fn, (fn(init, head)), rest)
}
/*
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
*/
func foldr(fn FuncType, fini Any, list ConsType) Any {
if list == nil {
return fini
}
head, tail := Car(list), Cdr(list)
rest,_ := tail.(ConsType)
return fn(head, foldr(fn, fini, rest))
}
func walkDownList(list ConsType) {
var printer func(a, b Any) Any
printer = func(a, b Any) Any {
fmt.Println(a)
if b == nil {
fmt.Println('m')
return nil
}
if bVal, castOk := b.(ConsType); castOk {
bVal(printer)
}
return nil
}
list(printer)
}
func main() {
fmt.Println("Hello, playground")
a := Cons(12, 13)
print2 := func(x,y Any) Any { return fmt.Sprintf("(%v,%v)",x,y) }
add2:= func(x, y Any) Any {
fmt.Println("...", x,y)
return x.(int) + y.(int)
}
fmt.Println(a(add2))
list := Cons(12, Cons(14, Cons(16, nil)))
fmt.Println(list, '!')
walkDownList(list)
fmt.Println("..",foldl(add2, 0, list))
fmt.Println("!!",foldr(add2, 0, list))
fmt.Println("..",foldl(print2, 0, list))
fmt.Println("!!",foldr(print2, 0, list))
}
@svanellewee
Copy link
Author

@svanellewee
Copy link
Author

Output:

Hello, playground
... 12 13
25
0x211c0 33
12
14
16
109
... 0 12
... 12 14
... 26 16
.. 42
... 16 0
... 14 16
... 12 30
!! 42
.. (((0,12),14),16)
!! (12,(14,(16,0)))

Program exited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment