Message from discussion
Intuitive explanation of currying.
Received: by 10.224.176.67 with SMTP id bd3mr8009213qab.8.1343000120285;
Sun, 22 Jul 2012 16:35:20 -0700 (PDT)
Received: by 10.66.89.199 with SMTP id bq7mr1367358pab.32.1343000023122;
Sun, 22 Jul 2012 16:33:43 -0700 (PDT)
Path: a15ni59622917qag.0!nntp.google.com!x2no6004870qaj.0!news-out.google.com!p10ni34696409pbh.1!nntp.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!ctu-peer!ctu-gate!news.nctu.edu.tw!usenet.stanford.edu!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From: =?KOI8-R?B?98HTyczJyiD3z9LPzsvP1w==?= <ba...@voronkov.name>
Newsgroups: comp.lang.functional
Subject: Re: Intuitive explanation of currying.
Date: Mon, 16 Jul 2012 07:12:25 -0700 (PDT)
Organization: http://groups.google.com
Lines: 163
Message-ID: <ab2fa618-e841-48dc-8804-bccd1ffa4948@googlegroups.com>
References: <jtufst$fgq$1@dont-email.me>
NNTP-Posting-Host: 95.143.213.226
Mime-Version: 1.0
X-Trace: posting.google.com 1342447945 24821 127.0.0.1 (16 Jul 2012 14:12:25 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 16 Jul 2012 14:12:25 +0000 (UTC)
In-Reply-To: <jtufst$fgq$1@dont-email.me>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=95.143.213.226;
posting-account=sxKr-woAAAA4yGx_aYaT9sXsGM0LWgSB
User-Agent: G2/1.0
Bytes: 7762
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Your explanation doesn't really explain currying. It actually explains the =
whole concept of partial application. Currying is in fact a very simple con=
cept.
Imagine, that we have language where a function can be applied only to one =
argument - there are no multiple argument functions at all. What we can do =
in such situation to imitate multiple argument functions? We can write func=
tions that return other functions.
For example, a function for two arguments is a function for one argument th=
at take this argument and returns another function for one argument. Here i=
s an example in JavaScript:
function sum(x) {
return (function(y) { return x+y });
}
Applying:
sum(2)(2)
Or in a sane language:
sum =3D \x -> \y -> x+y
Applying:
sum 2 2 --That's equivalent to '(sum 2) 2' because of application left asso=
ciativity
Of course, for this trick to work our language should support closures (bec=
ause the nested function capture a argument of its parent functon). But if =
you understand closures it shouldn't be a problem for you.
On Sunday, July 15, 2012 5:17:19 PM UTC+4, (unknown) wrote:
> I was reading/reminded that 'lambda calculus is not easy'.
> So I a got-out my copy of
> http://en.wikipedia.org/wiki/Lambda_calculus
> But I can't afford the effort to learn how to edit wikis.
> So I'll put it here:- [my wiki contaminated by my 'comments']
>=20
> > Currying may best grasped intuitively through the use of an
> > example. Compare the function (x, y) | x*x + y*y with its curried
> > form, x | (y | x*x + y*y). Given two arguments, we have:
> > ((x, y) | x*x + y*y)(5, 2) =3D 5*5 + 2*2 =3D 29. <-- f(5, 2)=
=3D 29
> > However, using currying, we have:
> > ((x | (y | x*x + y*y))(5)) (2) <--- (functn) (arg)
> > =20
> > (<1 arg> maps-to <lambda1Funct> (5) ) (2)
> >=20
> > =3D (y | 5*5 + y*y) (2)
> > =3D 5*5 + 2*2 =3D 29 <----------------- * !!
> > and we see the uncurried and curried forms compute the same resul=
t.
> >=20
> So what? That's not an intuitive explanation.
> Intuitive means related to concrete/familiar life.
>=20
> Eg. take a 3D/volume: semi-sphere, rect-cube or a 'wedge' is=20
> interesting; and place it on your x-y-coordinate 'pad'.
>=20
> The task/function is to know the <heights above your x-y-pad>
> of the top-surface. Like the heights of a mountain at a specific
> longtitude, latitude.
>=20
> For the wedge: if you know the x-value and the the y-value
> is constant at THAT x-value, you've got the answer.
>=20
> For the mountain: if you can only buy services of a flying-saucer
> that gives the longtitude-to-terain-height-function for a FIXED
> latitude, you've got the height, from you single parameter.
>=20
> So the set of (x,y) tuples & f1 is collaped into an eqivalent:
> [say] (x, y-cuts/slices/function) . =20
> By induction, it extends to N-dimensions.
>=20
> Here's a very intuitive example:
> the fare for transport is quoted in a table, as being a function of
> 'route' and class. Where route means 'A to B' equals '=
;B to A'.
> So 'Albany to Boston', class:2 costs $7,
> and 'Boston to Albany', class:1 costs $9.
>=20
> If you want to know the fare from 'Albany to Cooktown', class:3,
> you don't need to provide both paramenters: 'route' and class=
.
> You can just give the route, and ask for the singleton-function=20
> [list of all prices for 'Albany to Cooktown' for all classes].
>=20
> Thanks, I think I understand currying better now.
> Do you?
>=20
> =3D=3D Chris Glur.
>=20
> PS. explanations that I have of 'monads' are disasterous.
On Sunday, July 15, 2012 5:17:19 PM UTC+4, (unknown) wrote:
> I was reading/reminded that 'lambda calculus is not easy'.
> So I a got-out my copy of
> http://en.wikipedia.org/wiki/Lambda_calculus
> But I can't afford the effort to learn how to edit wikis.
> So I'll put it here:- [my wiki contaminated by my 'comments']
>=20
> > Currying may best grasped intuitively through the use of an
> > example. Compare the function (x, y) | x*x + y*y with its curried
> > form, x | (y | x*x + y*y). Given two arguments, we have:
> > ((x, y) | x*x + y*y)(5, 2) =3D 5*5 + 2*2 =3D 29. <-- f(5, 2)=
=3D 29
> > However, using currying, we have:
> > ((x | (y | x*x + y*y))(5)) (2) <--- (functn) (arg)
> > =20
> > (<1 arg> maps-to <lambda1Funct> (5) ) (2)
> >=20
> > =3D (y | 5*5 + y*y) (2)
> > =3D 5*5 + 2*2 =3D 29 <----------------- * !!
> > and we see the uncurried and curried forms compute the same resul=
t.
> >=20
> So what? That's not an intuitive explanation.
> Intuitive means related to concrete/familiar life.
>=20
> Eg. take a 3D/volume: semi-sphere, rect-cube or a 'wedge' is=20
> interesting; and place it on your x-y-coordinate 'pad'.
>=20
> The task/function is to know the <heights above your x-y-pad>
> of the top-surface. Like the heights of a mountain at a specific
> longtitude, latitude.
>=20
> For the wedge: if you know the x-value and the the y-value
> is constant at THAT x-value, you've got the answer.
>=20
> For the mountain: if you can only buy services of a flying-saucer
> that gives the longtitude-to-terain-height-function for a FIXED
> latitude, you've got the height, from you single parameter.
>=20
> So the set of (x,y) tuples & f1 is collaped into an eqivalent:
> [say] (x, y-cuts/slices/function) . =20
> By induction, it extends to N-dimensions.
>=20
> Here's a very intuitive example:
> the fare for transport is quoted in a table, as being a function of
> 'route' and class. Where route means 'A to B' equals '=
;B to A'.
> So 'Albany to Boston', class:2 costs $7,
> and 'Boston to Albany', class:1 costs $9.
>=20
> If you want to know the fare from 'Albany to Cooktown', class:3,
> you don't need to provide both paramenters: 'route' and class=
.
> You can just give the route, and ask for the singleton-function=20
> [list of all prices for 'Albany to Cooktown' for all classes].
>=20
> Thanks, I think I understand currying better now.
> Do you?
>=20
> =3D=3D Chris Glur.
>=20
> PS. explanations that I have of 'monads' are disasterous.