其他
登顶Hacker News!国产编程语言MoonBit(月兔)来了
生成与现有解决方案相比显著更小的 WASM 文件
更高的运行时性能
先进的编译时性能
简单但实用的数据导向语言设计
fib
函数:// Moonbit
func fib(num : Int) -> Int {
fn aux(n, acc1, acc2) {
match n {
0 => acc1
1 => acc2
_ => aux(n - 1, acc2, acc1 + acc2)
}
}
aux(num, 0, 1)
}
// Go
func fib(n int) int {
var aux func(n, acc1, acc2 int) int
aux = func(n, acc1, acc2 int) int {
switch n {
case 0:
return acc1
case 1:
return acc2
default:
return aux(n-1, acc2, acc1 + acc2)
}
}
return aux(n, 0, 1)
}
// Rust
fn fib(n: i32) -> i32 {
fn aux(n: i32, acc1: i32, acc2: i32) -> i32 {
match n {
0 => acc1,
1 => acc2,
_ => aux(n - 1, acc2, acc1 + acc2),
}
}
aux(n, 0, 1)
}
基准测试结果:
https://moonbitlang.github.io/moonbit-docs/benchmark/fibonacci/
相关链接:https://zh.moonbitlang.com/
往期推荐
点这里 ↓↓↓ 记得 关注✔ 标星⭐ 哦