recursion - SML currying question -


i have midterm coming next week , going on sml notes provided in class. came across currying example , not sure how worked.

it simple function computes power of number. here function definition:

fun pow 0 n = 1 | pow k n = n*pow(k-1)n 

i'm not sure how function works when pass following arguments:

val x = pow 2 2 

this way see it:

=2*pow(1)2 =2*(2*pow(0)2)2 =2*(2*(1)2)2)  

the result should getting 4 don't see how result steps have carried out above.

help please. thank-you.

ah, standard ml of new jersey, how miss thee...

anyway, let me go through step step. remember currying, unlike dinner in front of me (which incidentally curry dish), way of dealing 1 argument @ time return new function. in mind, apply first 2 given function. since 1 pattern matches, have new function -- let's call "curry":

curry n = n * pow 1 n 

note have "inner" version of pow function address. doing so, again, 1 pattern matches. let's call inner curried function "rice":

rice n = n * pow 0 n 

and 1 more, "shrimp" -- time, other pattern matches:

shrimp n = 1 

the recursion has terminated here, have:

rice n = n * 1 curry n = n * (n * 1) 

now, use second 2 in original pow 2 2 new curry function:

curry 2 = 2 * (2 * 1) 

which of course 4.

i highly doubt sml names curried functions in way, hope understand concept. take no responsibility if makes hungry.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -