Partial function application: Difference between revisions
Content deleted Content added
→{{header|TXR}}: Added. |
|||
Line 1,295: | Line 1,295: | ||
2 4 6 8 ==f2==> 4 16 36 64 |
2 4 6 8 ==f2==> 4 16 36 64 |
||
</pre> |
</pre> |
||
{{header|TXR}} |
|||
The <code>f1</code> function is just <code>mapcar</code>, or at least the oft-used special case thereof when there is only one list to process and the function takes one argument. |
|||
The <code>op</code> operator in TXR Lisp is used for partial application. |
|||
Multiply every element of a list by two, by partially applying <code>2</code> to the <code>*</code> operator: |
|||
<lang txr>(mapcar (op * 2) '(1 2 3))</lang> |
|||
Now that is not a function, but a function call. If we want a one-argument function in which the <code>mapcar</code> function itself is curried, we use <code>op</code> again: |
|||
<lang txr>(op mapcar (op * 2))</lang> |
|||
This could be applied to the list like this to produce <code>(2 4 6)</code>: |
|||
<lang txr>[(op mapcar (op * 2)) '(1 2 3)]</lang> |
|||
We could define names for these functions like the problem statement says, but that defeats much of the point of partial application: |
|||
<lang txr>(defun fsf1 (list) |
|||
[(op mapcar (op * 2)) '(1 2 3)]) ;; pointless: why don't we just call mapcar?</lang> |
|||
Here is an <code>op</code> expressino that returns a function which squares the elements of a sequence, producing a new sequence. Since we use the multiplication function, we must repeat an argument and so we must use the <code>@number</code> syntax supported by <code>op</code>: |
|||
<lang txr>(op mapcar (op * @1 @1))</lang> |
|||
This is an anonymous version of <code>fsf2</code>. |
|||
{{omit from|Euphoria}} |
{{omit from|Euphoria}} |
||
{{omit from|Go|No way to define one function in terms of another.}} |
{{omit from|Go|No way to define one function in terms of another.}} |