Skip to content

Instantly share code, notes, and snippets.

View Mizzlr's full-sized avatar

Mushtaque Ahamed A Mizzlr

View GitHub Profile
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: "
// 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;
@Mizzlr
Mizzlr / NULL.js
Created June 16, 2016 08:41
Church Encoding of Null
var NULL = function(x) { return x;}
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;
};
function curry(f) {
return function (x) {
return function (y) {
return f(x,y);
};
};
};
function uncurry(f) {
return function (x,y) {
// 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);
var IF = function (test) {
return function (onTrue) {
return function (onFalse) {
return test(onTrue)(onFalse);
};
};
};
// example use
IF(TRUE)(trueBlock)(falseBlock);
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
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
# 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!