[RUBY] Representing Fibonacci sequences using lambda expressions in various languages

I had a chance and tried to express the Fibonacci sequence using lambda expressions in various languages.

javascript edition

fib.js


var fib = function(v){
  return(function(f,m){
    return f(f,m)
  }(function(r,n){
    return n < 2 ? n : r(r,n-1)+r(r,n-2)
  },v)
)}

python edition

python


$ python
>>> fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(6)
8

ruby edition

python


$ irb
irb(main):001:0> f = lambda { |n| n < 2 ? n : f.call(n-1) + f.call(n-2) }
$ irb(main):002:0> f.call(0)
0
$ irb(main):003:0> f.call(1)
1
$ irb(main):004:0> f.call(2)
1
$ irb(main):005:0> f.call(6)
8

Since ruby 1.9, the way to write lambda function has increased, so you can also write as follows (thanks @ kbaba1001)

[1] pry(main)> f = ->(n) { n < 2 ? n : f[n-1] + f[n-2] }
=> #<Proc:0x007fb5ac26fe58@(pry):1 (lambda)>
[2] pry(main)> f[0]
=> 0
[3] pry(main)> f[1]
=> 1
[4] pry(main)> f[2]
=> 1
[5] pry(main)> f[3]
=> 2
[6] pry(main)> f[6]
=> 8

haskell

python


$ ghci
Prelude> let fib = 0:1:zipWith (+) fib (tail fib)
Prelude> fib !! 0
0
Prelude> fib !! 1
1
Prelude> fib !! 2
1
Prelude> fib !! 6
8

c ++ edition

Source code

fib.cc


#include <iostream>
#include <functional>
using namespace std;

int main(int argc, char const *argv[])
{
  std::function<int(int)> fib = [&fib](constintn)  {
    return n < 2 ? n : fib(n - 1) + fib(n - 2);
  };

  cout << fib(0) << endl;
  cout << fib(1) << endl;
  cout << fib(6) << endl;

  return 0;
}

Execution procedure

python


$ g++ -std=c++11 fib.cc 
$ ./a.out 
1
1
8

vim edition

Updated to vim 8.0

python


$ apt-get install software-properties-common # if not found add-apt-repository command
$ sudo add-apt-repository ppa:jonathon/vim
$ sudo apt-get update
$ sudo apt-get install vim
$ vim --version
$ sudo add-apt-repository -remove ppa:jonathon/vim # delete PPA if needed 

Source code

fib.vim


function! Main()
  let Y = {f -> (({x -> f ({y -> x(x)(y)})})({x -> f ({y -> x(x)(y)})}))}
  let Fib = {f -> {n -> (n < 2 ? n : f(n-1) + f(n-2))}}
  echo Y(Fib)(0)
  echo Y(Fib)(1)
  echo Y(Fib)(2)
  echo Y(Fib)(6)

Execution procedure

python


$ vim
:source fib.vim
:call Main()

Impressions

Even one way of writing a lambda expression is unique depending on the language. Personally, python and ruby were intuitive. I managed to get Haskell, javascript, and c ++, but I wasn't completely familiar with the grammar. I wrote vim recommended by people in the workplace. I had the hardest time, but I was very impressed when I was able to do it.

If you have any other unique lambda expressions, please let me know :)

Recommended Posts

Representing Fibonacci sequences using lambda expressions in various languages
HMAC in various languages
[AOJ] Descending sort in various languages
When using regular expressions in Python
Try to display the Fibonacci sequence in various languages in the name of algorithm practice
Write classes (or class-like) in various programming languages
Record YouTube views in a spreadsheet using Lambda