const log = console.log;

// zero :: &fa.a
const zero = f => x => x; // zero is F
// once :: &fa.fa
const once = f => x => f(x); // once it I
// twice :: &fa.f(fa)
const twice = f => x => f(f(x));
// thrice :: &fa.f(f(fa))
const thrice = f => x => f(f(f(x))); const T = true;
const F = false;
const I = x => x;
const not = x => !x;
const K = x => y => x log(zero(not)(T)) // true, because only return second arguement
log(once(not)(T)) // false
log(twice(not)(F)) // false
log(thrice(not)(T)) // false log('****') /** SUCCSOR
SUCC N1 = N2
SUCC N2 = N3
SUCC(SUCC N1) = N3 SUCC &fa.fa = &fa.f(fa)
SUCC N2, then n is 2, do f n times, then add one f more
*/
const _succ = n => f => x => f(n(f)(x));
// conver chunch number to JS number.
// jsnum :: take a chunch number, call (x => x + 1) n times, and start from 0.
const jsnum = n => n(x => x + 1)(0);
log(_succ(zero)(not)(T)) // false
log(jsnum(_succ(zero))) // 1
log(jsnum(_succ(_succ(zero)))) // 2 const n0 = zero;
const n1 = once;
const n2 = twice;
const n3 = thrice;
const n4 = _succ(thrice); log(jsnum(_succ(n2))) // 3 const B = f => g => a => f(g(a)); const succ = n => f => B(f)(n(f));
// Add N1 N4 = succ(N4)
// Add N2 N4 = succ(succ(N4))
// Add N3 N4 = succ(succ(succ(N4)))
// Add N3 N4 = (succ.succ.succ) N4 === N3 succ N4
const add = n => k => n(succ)(k);
console.log(jsnum(add(n3)(n4))); // 7 const mult = B; // mult = B
console.log(jsnum(mult(n2)(n3))) // Thrush $af.fa = CI (Cardinal Idiot, flip the arguements)
const pow = n => k => k(n);
console.log(jsnum(pow(n2)(n3))); // 8 // isZero :: $n.n(f)(args)
// is n = 0, f won't run, just return args
// Then args should be T
// $n.n(f)(T), now if n > 0, f will be run,
// we want it always return F
// K(F), constant(F)
// $n.n(K(F))(T)
const isZero = n => n(K(F))(T)
console.log(isZero(n0)) // true
console.log(isZero(n1)) // false

 succ :: Doing N + 1 times fn.

add :: Doing N times succ, based on K

mult :: is B

pow :: or Thrush, is flip

isZero :: return just T otherwise K(F) , K is constant

[Functional Programming] Add, Mult, Pow, isZero的更多相关文章

  1. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  2. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  3. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  4. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

  5. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  6. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  7. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  8. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  9. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

随机推荐

  1. Git基本理解

    1.版本控制 Git 是一个分布式版本控制系统 (Distributed Version Control System - DVCS). 所谓版本控制,意思就是在文件的修改历程中保留修改历史,让你可以 ...

  2. 数据去重 -uniq

    也是经常和管道 一起操作的. -c  每行出现的次数 -d  仅仅显示重复行 -u  仅仅显示不重复行 -i  忽略大小写 注: 在对字母进行去重的时候, 一定要先对文件进行排序之后,去重的功能才可以 ...

  3. @click.prevent.self和@click.self.prevent区别

    注意:prevent 阻止的是“跳转事件”而不是“弹出警告” v-on:click.prevent.self的demo如下: <div id="box"> <di ...

  4. 初遇PHP(一)

    因为想给自己弄一个微信公众号,顺便提升一下自己,所以有了以下内容,本次学习的最终目标是能用php制作套微信公众号,然后转成Java.为什么要这么麻烦呢,其一是买的资料书是php的,其二是顺水推舟刚好可 ...

  5. 网易Java程序员两轮面试,这些问题你能答对几个?

    一转眼,2018 年已经过去了,你是否在满意的公司?拿着理想的薪水? 虽然"钱多.事少.离家近"的工作可能离技术人比较远,但是找到一份合适的工作,其实并不像想象中那么难.但是,有些 ...

  6. ale.js 对比其他框架

    欢迎!我们相信你来这里的目的就是为了解 Ale 与其他大型框架的区别,这也正是我们想要在此回答的. 客观来说,作为 Ale 的核心开发者,我们肯定会更偏爱 Ale,认为对于某些问题来讲用 Ale 解决 ...

  7. JS中的继承(上)

    JS中的继承(上) 学过java或者c#之类语言的同学,应该会对js的继承感到很困惑--不要问我怎么知道的,js的继承主要是基于原型(prototype)的,对js的原型感兴趣的同学,可以了解一下我之 ...

  8. 【二分】Shell Pyramid

    [来源]:2008年哈尔滨区域赛 [题目链接]: http://acm.hdu.edu.cn/showproblem.php?pid=2446 [题意] 题目是真的长呀,其实就问一个问题. 按照图里面 ...

  9. ModbusRtu通信报文详解【一】

    Modbus协议可谓是工业控制领域应用最广泛的协议之一.根据不同的电气接口,包括Modbus Rtu/ASCII,Modbus TCP/UDP,从学习的角度来说,只要学会其中一种,剩余的都是大同小异的 ...

  10. whistle学习(二)之启动、停止、重启、更新whistle等命令

    新版本的whistle支持三种等价命令whistle,w2,wproxy 启动whistle w2 start 启动时指定端口 w2 start -p (// 不设置端口默认使用8899) 默认端口为 ...