[Functional Programming] Church Encodings: Numberals
Programming functional
2023-09-14 09:00:48 时间
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] Discard the High and Low Values From an Array of Numbers with Ramda
- [Kotlin] Compare Functional Programming in Java and Kotlin
- [Functional Programming] Function modelling -- 10. Free Monads
- [Functional Programming] Use a Javascript Array to Construct a Maybe
- [Functional Programming] Church Encodings: Numberals
- [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
- [Functional Programming] Arrow Functor with contramap
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
- [Functional Programming] Find max / min value by using Ramda
- [Kotlin] Compare Functional Programming in Java and Kotlin
- [Functional Programming] Alternative operator for Either
- [Functional Programming] Arrow Functor with contramap
- [Algorithm] Meeting hour optimization (Kanpsack problem) and Dynamic programming
- [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
- 如何基于 Restful ABAP Programming 模型开发并部署一个支持增删改查的 Fiori 应用
- 【编程实践】什么是计算机编程?定义软件开发。What is Computer Programming? Defining Software Development.
- BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest