CSC/ECE 517 Fall 2007/wiki1 3 c1: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
mNo edit summary
Line 2: Line 2:
== Currying ==
== Currying ==
'''
'''


Currying is a technique in functional programming. It is based on a very simple mathematical concept that:
Currying is a technique in functional programming. It is based on a very simple mathematical concept that:

Revision as of 21:52, 13 September 2007

Currying

Currying is a technique in functional programming. It is based on a very simple mathematical concept that:

lambda { |a, b, c| ... } lambda { |a| lambda { |b| lambda { |c| ... } } } or lambda { |a| lambda { |b, c| ... } }

In other words, a function’s arguments can be broken down the way liked by the programmer. We can apply a three-argument function to two arguments to get a one-argument function back. The name "currying" was coined by Christopher Strachey in 1967, is a reference to logician Haskell Curry.

Currying can be done explicitly with lambdas—say we want to curry two arguments into a three-argument function.

lambda { |c| zoog( 1, 2, c ) }

Two arguments curried in, one argument still dangling.

Example 1 Let us consider a site which allows us to play music files. Now, for each music file we play, the relevant information to be passed to play the system is Album Name, Track Number and Player to be used by that site. Let’s consider a scenario where a music site uses only one type of player every time. Let’s say the sites and player is – Mp3 Site (Jukebox Player). Instead of passing the same Player Name every time to the site, we can curry the player Name in the function Play and thus we’ll be sending the only minimum necessary parameters required to play the file while the site-specific information, i.e. the player name will be curried.

def play(albumname,songname,player)

     @albumname = albumname
     @Trackname = songname
     @player = player
     puts "The album is #{@albumname} Track is #{@Trackname} and Player is #{@player}"
 end

Using Curying mp3site = lambda{|albumname,songname| play(albumname,songname,"Jukebox")}

Input Without Using Currying play("The Best of Bryan Adams", "Track 7", "Jukebox") play("MLTR", "Track 1"," Jukebox ")

Input Using Currying mp3site.call("The Best of Bryan Adams", "Track 7") mp3site.call("MLTR", "Track 1")


Example 2 The following program illustrates the concept of Currying using a simple example of a basic calculator. The method calculator accepts two operands var1 & var2 performs the operation specified by 'op' on the two operands and returns the result. The method 'calculate' thus accepts 3 arguments. Now, calling this method again and again would lead to poor readbility. Instead by implementing currying we can improve the readability of the code. In the following code we have made use of the 'calculate' method with currying as well as without using curring. Places where 'calculate' is called using calculate(3,+,4) are instances where currying is not used.Places where either of 'add','sub','mul','div' have been used implements currying.

definition of 'calculate' method def calculate(var1,op,var2)

 if op == '+'
   return puts "The Result of addition is      : #{var1+var2}"
 elsif op == '-'
   return puts "The Result of subtraction is   : #{var1-var2}"
 elsif op == '*'
   return puts "The Result of multiplication is: #{var1*var2}"
 elsif op == '/'
   return puts "The Result of division is      : #{var1/var2}"
 else
   return puts "wrong choice ! "
 end

end

Following 'calculate' method calls are without using currying

 calculate(3,'+',4)
 calculate(4,'-',5)
 calculate(4,'*',2)
 calculate(4,'/',2)

Currying Implementation using 'lambda'

 add = lambda {|x , y| calculate(x,'+',y)}
 sub = lambda {|x , y| calculate(x,'-',y)}
 mul = lambda {|x , y| calculate(x,'*',y)}
 div = lambda {|x , y| calculate(x,'/',y)} 

Following method calls are by using currying

 add.call(3,4)
 sub.call(4,5)
 mul.call(4,2)
 div.call(4,2)

Features of Currying

1.The above example illustrates that currying helps improve readability of code and therefore its maintainability.

2. Results without currying and with currying are same.

3.Also the reliability if the code increases since we are reducing the number of parameters to be passed, i.e. the user is less prone to make mistakes in using the method.