Talk:Partial function application: Difference between revisions

From Rosetta Code
Content added Content deleted
 
(Common Lisp.)
Line 9: Line 9:


Any programmers, especially those with functional as well as none functional language experience care to comment? --[[User:Paddy3118|Paddy3118]] 02:28, 26 March 2011 (UTC)
Any programmers, especially those with functional as well as none functional language experience care to comment? --[[User:Paddy3118|Paddy3118]] 02:28, 26 March 2011 (UTC)

:With [[Common Lisp]], the explicit currying seems to be the only way to do the partial application. I try to show this with code. This is <tt>mapcar</tt> with 2 arguments.

:<lang lisp>CL-USER> (defun f1 (n) (* n 2))
F1
CL-USER> (mapcar #'f1 '(2 3 5 7))
(4 6 10 14)</lang>

:I can use <tt>lambda</tt> for the explicit currying, like one would in Python.

:<lang lisp>CL-USER> ((lambda (s) (mapcar #'f1 s)) '(2 3 5 7))
(4 6 10 14)</lang>

:I can also define a <tt>partial</tt> function for the partial application; but it uses the explicit currying to do so.

:<lang lisp>CL-USER> (defun partial (f &rest args)
(lambda (&rest args2) (apply f (append args args2))))
PARTIAL
CL-USER> (funcall (partial #'mapcar #'f1) '(2 3 5 7))
(4 6 10 14)</lang>

:There is not much reason to write <tt>(partial #'mapcar #'f1)</tt> instead of <tt>(lambda (s) (mapcar #'f1 s))</tt>. This would be like writing <tt>sum((2, 3))</tt> instead of <tt>2 + 3</tt> in Python. --[[User:Kernigh|Kernigh]] 02:34, 1 April 2011 (UTC)

Revision as of 02:34, 1 April 2011

Explicit curry vs Partial application

In Python there is a clear difference. Here's explicit currying:

<lang python>def fsf1(fs, f1):

   return lambda s: fs(f1, s)</lang>

I would like to tie this task down, but now recognise that it may be difficult.

Any programmers, especially those with functional as well as none functional language experience care to comment? --Paddy3118 02:28, 26 March 2011 (UTC)

With Common Lisp, the explicit currying seems to be the only way to do the partial application. I try to show this with code. This is mapcar with 2 arguments.
<lang lisp>CL-USER> (defun f1 (n) (* n 2))

F1 CL-USER> (mapcar #'f1 '(2 3 5 7)) (4 6 10 14)</lang>

I can use lambda for the explicit currying, like one would in Python.
<lang lisp>CL-USER> ((lambda (s) (mapcar #'f1 s)) '(2 3 5 7))

(4 6 10 14)</lang>

I can also define a partial function for the partial application; but it uses the explicit currying to do so.
<lang lisp>CL-USER> (defun partial (f &rest args)

(lambda (&rest args2) (apply f (append args args2)))) PARTIAL CL-USER> (funcall (partial #'mapcar #'f1) '(2 3 5 7)) (4 6 10 14)</lang>

There is not much reason to write (partial #'mapcar #'f1) instead of (lambda (s) (mapcar #'f1 s)). This would be like writing sum((2, 3)) instead of 2 + 3 in Python. --Kernigh 02:34, 1 April 2011 (UTC)