Daniel’s Blog

Ruby & Haskell pt 2

March 21st, 2007 by daniel

This post is a bit of an update of this one

The Ruby can stay the same:
search_engines = %w[Google Yahoo MSN].map do |engine| "https://www." + engine.downcase + ".com"
end

But we can refine the Haskell even further than I originally posted, by saying the following:

search_engines = map (\engine -> "https://www." ++ downcase engine ++ ".com") ["Google","Yahoo","MSN"] where downcase = map toLower

The above uses a lambda expression (\) to state that engine comes in later… its quite hard to explain lambda expressions unless you have done functional programming. Basically you can state that a function takes in something without actually saying that it takes in something. Graham Huttons book and Simon Thompsons books are good references to check it out for Lambda Calculus. In both the Ruby and Haskell implementations we are using lambda expressions, in this example engine is our term (key).

(Thanks to Ian Bayley for providing me with some hints about this)

(p.s. in the Haskell implementation, toLower is a character function… which if you are using some newer versions of Hugs you will need to add “import Char” to the top of your hs/lhs program if you want to use it)

Technorati Tags: haskell, ruby, functional, programming

No Comments

<!--<div id="semanticbrowsing" style="text-align:center;"> OpenLink RDF Browser 1 OpenLink RDF Browser 2 Zitgist Data Viewer <area shape="rect