Higher-order functions: Difference between revisions
Content added Content deleted
m (→{{header|Quackery}}: added some more commentary) |
|||
Line 3,393: | Line 3,393: | ||
$result = first(function() { return 'second'; });</lang> |
$result = first(function() { return 'second'; });</lang> |
||
=={{header|Picat}}== |
|||
<lang Picat>go => |
|||
L = 1..10, |
|||
L2 = 1..3, |
|||
% map |
|||
println(map(f1,L)), |
|||
println(map($f2(3),L)), |
|||
println(map(f2,L,map(f1,L))), |
|||
% List comprehension. |
|||
% This is the recommended approach. |
|||
println([f1(I) : I in L]), |
|||
println([[I,J,f2(I,J)] : I in L, J in L2]), |
|||
% apply |
|||
println(apply(+,1,2)), |
|||
println(apply(f2,10,22)), |
|||
% sort function |
|||
S = [ |
|||
"rosetta code", |
|||
"adam", |
|||
"eve", |
|||
"picat", |
|||
"pattern-matching", |
|||
"imperative", |
|||
"constraints", |
|||
"actors", |
|||
"tabling" |
|||
], |
|||
println(map(len,S)), |
|||
println(S.qsort(sortf)), % sort on length |
|||
nl. |
|||
f1(X) = X**2. |
|||
f2(X,A) = X**A + A**X. |
|||
% |
|||
% qsort(List, SortFunction) |
|||
% returns a sorted list according to the sort function SortFunction |
|||
% |
|||
qsort([],_F) = []. |
|||
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F) |
|||
++ [H] ++ |
|||
qsort([E : E in T, not call(F,E,H)],F). |
|||
% sort on length |
|||
sortf(F1,F2) => |
|||
F1.length < F2.length.</lang> |
|||
Output: |
|||
<pre>[1,4,9,16,25,36,49,64,81,100] |
|||
[4,17,54,145,368,945,2530,7073,20412,60049] |
|||
[2,32,20412,4295032832,298023223886718750,10314424798490535548348731392,256923577521058878088611477224913844394456,6277101735386680763835789423207666416102355725939011223552,196627050475552913618075908526912116283103450944214766927315565632601688195930,10000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000] |
|||
[1,4,9,16,25,36,49,64,81,100] |
|||
[[1,1,2],[1,2,3],[1,3,4],[2,1,3],[2,2,8],[2,3,17],[3,1,4],[3,2,17],[3,3,54],[4,1,5],[4,2,32],[4,3,145],[5,1,6],[5,2,57],[5,3,368],[6,1,7],[6,2,100],[6,3,945],[7,1,8],[7,2,177],[7,3,2530],[8,1,9],[8,2,320],[8,3,7073],[9,1,10],[9,2,593],[9,3,20412],[10,1,11],[10,2,1124],[10,3,60049]] |
|||
3 |
|||
10000000026559922791424 |
|||
[12,4,3,5,16,10,11,6,7] |
|||
[eve,adam,picat,actors,tabling,imperative,constraints,rosetta code,pattern-matching]</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |