This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
-- recursive square root function | |
rsqrt x n = if n == 1 then sqrt x else sqrt (x + rsqrt x (n-1)) | |
-- product expansion of pi upto n terms | |
productPi n = 2 * product [ 2 / (rsqrt 2 i) | i <-[1..n]] | |
main = do | |
putStr "Actual value of pi: " |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a function that take one argument x and returns itself | |
function(x) { return x;} | |
// a function that take nothing; but this will not be used | |
// naming a function is do as | |
var functionName = function (x) { // do something with x | |
return x; | |
}; | |
// also | |
function functionName(x) { //... | |
return x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var NULL = function(x) { return x;} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var f1 = function(a,b){ | |
// do something with a and b | |
return a + b; | |
}; | |
var f2 = function(a){ | |
// can do something with a here | |
return function(b){ | |
// can do something with a and b here | |
return a + b; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function curry(f) { | |
return function (x) { | |
return function (y) { | |
return f(x,y); | |
}; | |
}; | |
}; | |
function uncurry(f) { | |
return function (x,y) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// onTrue and onFalse are also some function | |
var TRUE = function (onTrue) { | |
return function (onFalse) { | |
return onTrue(VOID); | |
}; | |
}; | |
var FALSE = function (onTrue) { | |
return function (onFalse) { | |
return onFalse(VOID); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var IF = function (test) { | |
return function (onTrue) { | |
return function (onFalse) { | |
return test(onTrue)(onFalse); | |
}; | |
}; | |
}; | |
// example use | |
IF(TRUE)(trueBlock)(falseBlock); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function boolify (churchBoolean) { | |
return churchBoolean (function (_) { return true; }) | |
( function (_) { return false; }); | |
// function had to have one input argument | |
// because that is what is expected by TRUE or FALSE | |
// example | |
boolify(TRUE) // returns true | |
boolify(FALSE) // return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AND = function(bool1){ | |
return function(bool2){ | |
bool1(bool2)(bool1); | |
}; | |
}; | |
// consider what happens for all combination of bool1 and bool2 | |
// bool1(bool2)(bool1) = AND(bool1)(bool2) | |
// TRUE(FALSE)(TRUE) = FALSE ; TRUE returns the first argument | |
// TRUE(TRUE)(TRUE) = TRUE | |
// FALSE(TRUE)(FALSE) = FALSE ; FALSE returns the second argument |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The above functions in python | |
NULL = lambda x: x | |
TRUE = lambda onTrue: lambda onFalse: onTrue | |
FALSE = lambda onTrue: lambda onFalse: onFalse | |
IF = lambda test: lambda onTrue: lambda onFalse: test(onTrue)(onFalse) | |
AND = lambda bool1: lambda bool2: bool1(bool2)(bool1) | |
OR = lambda bool1: lambda bool2: bool1(bool1)(bool2) | |
NOT = lambda booly: booly(FALSE)(TRUE) | |
# Very readable and terse indeed! |