Control structure using pattern matching like Erlang

Prasath Ram

43 sec read

During the week end was trying to implement a control structure using pattern matching similar to erlang implementation in ruby
Here is my result in ruby for creating a quite familiar control structure for loop in ruby and erlang

[source language=”ruby”]
module Control_structure
def self.ruby_for(a, b, c)
return if pattern_matches? a, b
yield
ruby_for(a+c, b, c) {yield}
end
def self.pattern_matches? a, b
a == b || a > b
end
end
Control_structure.ruby_for(1, 10, +1) {puts 10}
prints 10 using the loop it works but lemme check this on erlang too
[/source]
Erlang
[source language=”ruby”]
-module(control_structure).
-export([erlang_for/1]).
erlang_for(Max, Max, F) -> [F(Max)];
erlang_for(I, Max, F) -> [F(I)|for(I+1, Max, F)].
[/source]
Test case for erlang
[source language=”ruby”]
control_structure:erlang_for(1, 10, fun(I) -> I end).
[/source]
on erlang am using a fun method which is a ananomous method used for writing higher methods
Result View
Even though i implemented the functionality in ruby
still my ruby code is large and its not easy configurable to make different large patterns
so literally fails but ended my thought with a good comparison

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *