zoukankan      html  css  js  c++  java
  • [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
    

      

  • 相关阅读:
    对于CD翻录的一些记录
    暑期实践
    暑期实践
    垃圾处理器-CMS
    离合器半联动点的判断和技巧
    Win10+VS2019+OpenCV环境配置
    C++ 学习资料
    科目二起步原理
    道路交通安全违法行为记分分值分类总结
    NWERC 2020 题解
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10844317.html
Copyright © 2011-2022 走看看