[Functional Programming] Church Encodings: Numberals
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; 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
[Functional Programming] Church Encodings: Numberals的更多相关文章
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- 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 ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...
随机推荐
- Oracle解决空表不导出
Select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0 or num_rows ...
- 安装consul
概述consul是google开源的一个使用go语言开发的服务发现.配置管理中心服务.内置了服务注册与发现框架.分布一致性协议实现.健康检查.Key/Value存储.多数据中心方要依赖其他工具(比如Z ...
- C# 反射遍历对象所有属性
[TestMethod] public void Test6() { List<RepaymentRecord> repaymentList = new List<Repayment ...
- table与json的互转
json是键值对,在Lua中类型是string 主要运用在table中.表:local t={a="1",b="2",c="3",d=&qu ...
- 谁是嫌疑犯问题Python枚举法
原文:https://blog.csdn.net/yunzifengqing/article/details/81941592 问题描述:有6名犯罪嫌疑人A.B.C.D.E.F,已知如下事实: A.B ...
- 【强化学习】MOVE37-Introduction(导论)/马尔科夫链/马尔科夫决策过程
写在前面的话:从今日起,我会边跟着硅谷大牛Siraj的MOVE 37系列课程学习Reinforcement Learning(强化学习算法),边更新这个系列.课程包含视频和文字,课堂笔记会按视频为单位 ...
- hadoop面试题(自己整理版)
1. hadoop 运行原理2. mapreduce 原理3. mapreduce 的优化4.举一个简单的例子说下 mapreduce 是怎么运行的5. hadoop 中 combiner 的作用6. ...
- 6 java 笔记
1 java的类通过构造器来创建该类的对象 2 java提供extends关键字来实现子类继承父类 3 初始化块总是在构造器调用之前被执行 4 可以吧java中的类当成一种自定义的类型 5 类定义的变 ...
- SQL:MYSQL入门
MYSQL(关系型数据库管理系统) 参考手册:http://www.w3school.com.cn/sql/index.asp 给大家讲一下数据库:常见的有 ACCESS.MSSSQL.MYSQL.O ...
- Java基础——2 操作符