[Functional Programming] Add, Mult, Pow, isZero
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的更多相关文章
- 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 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...
随机推荐
- GoJS
GoJS GoJS示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- Unknown custom element: <swiper>
刚开始使用VUE,一直提示这个,后来才发现是注册组件时注册反了:先新建VUE实例再注册组件是问题根源,调转一下顺序即可解决
- Active Learning 主动学习
Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习 版权声明:本文为博主原创文 ...
- Java Web ActiveMQ与WebService的异同
Webservice 和MQ(MessageQueue)都是解决跨平台通信的常用手段 一.WebService:用来远程调用服务,达到打通系统.服务复用的目的.是SOA系统架构——面向服务架构的体现. ...
- centos中mariadb的相关操作
Tip 1 在使用mariadb中启动服务报错 : Failed to start mariadb.service: Unit not found. 解决办法: yum install -y mari ...
- Stacey矩阵简介
1. Stacey 矩阵包含哪几个区域? 1区:Simple 第一个区域,需求明确,技术(解决方案)也确定,这类项目就是简单的项目(Simple):比如注册一个新公司,需求很明确,手续也很清楚,就那么 ...
- python 的面试题总汇
函数作用域; LEGB : L>E>G>B L : local函数内部作用域 E : enclosing函数内部与内嵌函数之间 G : global全局作用域 B : build-i ...
- SQL基础:语句执行顺序
SQL入门 select * from table; SQL实战题目 有下面一个表 t ,存储了每个商品类别的成交明细,我们需要通过下面这张表获取订单量大于10对应的类别,并从中取出订单量前3的商品类 ...
- Boost Graph Library materials
Needed to compute max flow in a project and found the official document of BGL to be rather obscure, ...
- docker 网络模式详解
一.前言 Docker作为目前最火的轻量级容器技术,有很多令人称道的功能,如Docker的镜像管理.然而,Docker同样有着很多不完善的地方,网络方面就是Docker比较薄弱的部分.因此,我们有必要 ...