[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 ...
随机推荐
- NIKKEI Programming Contest 2019-2 Task D. Shortest Path on a Line
Observations ① 从 $1$ 到 $N$ 的最短路一定是不走回头路的.所谓走回头路是指从序号大的点走到序号小的点. 证明:首先,任意从 $1$ 到 $N$ 的路径的最后一步一定不是回头路. ...
- Spring4学习回顾之路08- FactoryBean配置Bean
建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Description: */ pu ...
- DLL导出函数
使用DEF文件从DLL导出 模块定义(.def)文件时包含一个或多个描述DLL各种属性的Module语句的文本文件.如果不使用_declspec(dllexport)关键字导出DLL的函数,则DLL需 ...
- LC 752 Open the Lock
由于这个问题,涉及了很多知识,例如数据结构里面的哈希表,c++中的迭代器,因此,需要对于每一个疑惑逐一击破. 问题描述 You have a lock in front of you with 4 c ...
- python内置函数0-1
# a=bool(None)# print(a) class Foo: def __repr__(self): return 'bbbbbbb'f = Foo()ret = ascii(f)print ...
- kettle处理未发现hadoop插件问题
背景:将测试环境的中kettle转换传输到生产环境上出现hadoop插件无法获取的错误 Hadoop File Output 2.0 - ERROR (version 7.1.0.0-12, buil ...
- 深入理解计算机系统 第十一章 网络编程 part2 第二遍
客户端和服务器通过因特网这个全球网络来通信.从程序员的观点来看,我们可以把因特网看成是一个全球范围的主机集合,具有以下几个属性: 1.每个因特网主机都有一个唯一的 32 为名字,称为它的 IP 地址 ...
- wcf可以返回的类型有哪些
Windows Communication Foundation (WCF) 使用 DataContractSerializer 作为其默认的序列化引擎以将数据转换到 XML 并将 XML 转换回数据 ...
- Winfrom 定时锁屏
#region 锁屏 public struct LASTINPUTINFO { [MarshalAs(UnmanagedType.U4)] public int cbSize; [MarshalAs ...
- JS基础_嵌套的for循环
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...