Skip to content

Instantly share code, notes, and snippets.

@codedmart
Last active August 29, 2015 14:04
Show Gist options
  • Save codedmart/1f3e08c8a06b415b873b to your computer and use it in GitHub Desktop.
Save codedmart/1f3e08c8a06b415b873b to your computer and use it in GitHub Desktop.
Functional Randomness with Languages
package main
import "fmt"
type Lx func(int) int
type Fx func(int) Lx
type Mx func(int) Fx
func cmbSum(a int, b int) Mx {
return func(c int) Fx {
return func(d int) Lx {
return func(e int) int {
return (a + b + c + d) * e
}
}
}
}
func cmb(a Lx, b Lx) Lx {
return func(c int) int {
return b(a(c))
}
}
func inc(x int) int {
return x + 1
}
func sum(x int) int {
result := 0
for i := 0; i < x; i++ {
result += i
}
return result
}
func main() {
n := 333
x := cmb(inc, sum)(n)
a := cmbSum(x, x)
b := a(x)
c := b(x)
d := c(2)
fmt.Println(x)
// prints 55611
fmt.Println(cmbSum(x, x)(x)(x)(2))
// prints 444888
fmt.Println(d)
// prints 444888
}
cmbSumH :: Num a => a -> a -> a -> a -> a -> a
cmbSumH a b c d e = (a + b + c + d) * e
cmbH :: (t2 -> t1) -> (t1 -> t) -> t2 -> t
cmbH a b c = b(a(c))
incH :: Int -> Int
incH x = x + 1
sumH :: (Enum a, Num a) => a -> a
sumH x = sum [1..(x - 1)]
main = do
let n = 333
let x = cmbH incH sumH n
let a = cmbSumH x x
let b = a x
let c = b x
let d = c 2
print (x)
-- prints 55611
print (cmbSumH x x x x 2)
-- prints 444888
print d
-- prints 444888
object FunctionTest {
def cmbSum(a:Int)(b:Int)(c:Int)(d:Int)(e:Int) = (a + b + c + d) * e
def cmb[A, B](a: Int => A)(b: A => B)(c: Int) = b(a(c))
def inc(x:Int):Int = x + 1
def sum(x:Int):Int = (1 to (x - 1)).reduceLeft(_ + _)
def main(args: Array[String]) {
val n = 333
val x = cmb(inc)(sum)(n)
val a = cmbSum(x)(x)(_)
val b = a(x)(_)
val c = b(x)(_)
val d = c(2)
println(x)
// prints 55611
println(cmbSum(x)(x)(x)(x)(2))
// prints 444888
println(d)
// prints 444888
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment