[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

转载于:https://www.cnblogs.com/Answer1215/p/10854580.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 面向对象编写的有限群判定程序,其中包含了群的二元运算和加法操作: ```cpp #include<iostream> using namespace std; class Group { private: int *elements; int size; public: Group(int n) { size = n; elements = new int[n]; for(int i = 0; i < n; i++) elements[i] = i; } Group(const Group& g) { size = g.size; elements = new int[size]; for(int i = 0; i < size; i++) elements[i] = g.elements[i]; } ~Group() { delete[] elements; } int binaryOp(int x, int y) { return elements[x] ^ elements[y]; } int add(int x, int y) { return (x + y) % size; } int mult(int x, int y) { int result = 0; while(y > 0) { if(y & 1) result = add(result, x); x = add(x, x); y >>= 1; } return result; } bool isClosed() { for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { int k = binaryOp(i, j); bool found = false; for(int l = 0; l < size; l++) { if(elements[l] == k) { found = true; break; } } if(!found) return false; } } return true; } }; int main() { int n; cout<<"Enter the size of the group: "; cin>>n; Group g(n); if(g.isClosed()) cout<<"The group is closed."<<endl; else cout<<"The group is not closed."<<endl; int x, y; cout<<"Enter two elements to multiply: "; cin>>x>>y; cout<<"The product is: "<<g.mult(x, y)<<endl; return 0; } ``` 该程序与前一个程序类似,但是在 Group 类中增加了二元运算方法 binaryOp()、加法方法 add() 和乘法方法 mult()。其中,binaryOp() 方法使用了位运算符 ^,实现了群的二元运算;add() 方法实现了群的加法操作;mult() 方法实现了群的乘法操作,其中使用了位运算符 & 和移位运算符 >>。 主函数中,程序先读入群的大小,然后创建一个 Group 类的对象,并调用 isClosed() 方法判断群是否封闭。接着,读入两个元素并调用 mult() 方法计算它们的乘积,最后输出结果。 同样需要注意的是,这个程序只是一个简单的示例,实际应用中需要考虑更多的细节和特殊情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值