Created
December 24, 2011 21:11
-
-
Save VoQn/1518364 to your computer and use it in GitHub Desktop.
This file contains 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
this is sample code | |
-- factorial. n! = 1 * 2 * 3 * ... * ( n - 1) | |
fact :: Int -> Int = F fact | |
-- Functor function for factorial | |
F f x :: (Int -> Int) -> Int -> Int = 1 if 0 `is` x else x * f $ x - 1 | |
-- Instead, functions define without Type Binding. | |
-- But, it maybe do unexpected work | |
fact2 = F2 fact2 -- fact2 :: Num a => a -> a | |
F2 f x = 1 if 0 `is` x else x * f $ x - 1 -- F2 :: Num a => (a -> a) -> a -> a | |
-- those code compile to javascript sush as ... | |
/** | |
* @param {number} x | |
* @return {number} | |
*/ | |
function fact ( x ) { | |
return ( 0 == x ) ? 1 : x * fact ( x - 1 ); | |
} | |
/** | |
-- create nothing too much functor method. | |
-- compiler give code most smart solution | |
**/ | |
/** | |
* when, compile option --paformance | |
* compiler think tail call optimization and halting problem | |
* @param {!number} x | |
* @return {!number} | |
*/ | |
function fact2 ( x ) { | |
if ( typeof x != "number" || Math.abs( x % 1 ) != 0 ) { | |
throw new Error ( "Illegal Arguments Type. It's must be Integral" ); | |
} | |
if ( x < 0 ) { | |
throw new Error ( "Unexpected Infinite Loop : " + x ); | |
} | |
/** @type {!number} **/ | |
var _r = 1; | |
while ( x > 0 ) { | |
_r = x-- * _r; | |
} | |
return _r; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment