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
    

      

  • 相关阅读:
    4.19Java.util.Arrays类
    4.19Java数组的拷贝
    Inverse matrix of 4x4 matrix
    自言自语
    病了两天
    当年3ds max盗版光碟上的广告
    头晕的厉害
    复习了一下STL容器的知识
    一个简单的能处理MIPMAP的类
    空间变换代码,相当简洁优美
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10844317.html
Copyright © 2011-2022 走看看