[Functional Programming] Fst & Snd, Code interview question
2023-09-14 08:59:15 时间
cons(a, b)
constructs a pair, andcar(pair)
andcdr(pair)
returns the first and last element of that pair. For example,car(cons(3, 4))
returns3
, andcdr(cons(3, 4))
returns4
.Given this implementation of cons:
def cons(a, b): def pair(f): return f(a, b) return pair
Implement
car
andcdr
.
const Fst = P => { return P((a, _) => a); }; const Snd = P => { return P((_, b) => b); }; const Cons = (a, b) => ((f) => f(a, b)); const res = Fst(Cons(3,2)); console.log(res); // 3 const res1 = Snd(Cons(21, 22)); console.log(res1); // 22
相关文章
- RabbitMQ & 死信队列DLX & TTL+DLX实现延迟队列
- 08·灵魂前端工程师养成-HTML实践&手机调试
- Windows下创建自定义服务的正确姿势(Instrsrv&Srvany)「建议收藏」
- 微服务架构(java环境&tomcat)
- Stream & JDK8新特性
- 【ES三周年】+es快速入门&基础概念讲解
- 7 Papers & Radios | Hinton前向-前向神经网络训练算法;科学家造出「虫洞」登Nature封面
- 精益产品开发 —— 丰田生产系统 & 精益生产
- 7 Papers & Radios |华盛顿大学等开发媲美AlphaFold2的新工具RoseTTAFold
- 7 Papers & Radios | NLP新范式Prompt;用神经网络解决混合整数规划问题
- Spring&Mybaits数据库配置解惑
- 如何增加Oracle数据库的AMP值(amp值oracle)
- LAMP&LNMP自动化安装脚本代码