Call a function: Difference between revisions
Content added Content deleted
No edit summary |
Tag: Undo |
||
Line 2,104: | Line 2,104: | ||
end |
end |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
==={{header|EMal}}=== |
|||
<syntaxhighlight lang="emal"> |
|||
# an utility function to better solve the task |
|||
fun task = void by text about, fun code |
|||
writeLine(0U00b7 + " " + about) |
|||
code() |
|||
end |
|||
fun answer = void by var message do writeLine(" " + message) end |
|||
# few definitions |
|||
fun noArgumentsFunction = int by block do return 97 end |
|||
fun fixedArgumentsFunction = void by var a, var b do end |
|||
fun variadicFunction = void by text a, some var values do end |
|||
fun funArgumentFunction = var by fun f, var b do return f() + b end |
|||
task("Calling a function that requires no arguments", void by block |
|||
answer("Is supported.") |
|||
noArgumentsFunction() |
|||
end) |
|||
task("Calling a function with a fixed number of arguments", void by block |
|||
answer("Is supported.") |
|||
fixedArgumentsFunction(97, 3.14) |
|||
end) |
|||
task("Calling a function with optional arguments", void by block |
|||
answer("Not supported in EMal.") |
|||
end) |
|||
task("Calling a function with a variable number of arguments", void by block |
|||
answer("Variadic functions are supported.") |
|||
variadicFunction("mandatory", 97, 3.14) |
|||
variadicFunction("mandatory", 97) |
|||
end) |
|||
task("Calling a function with named arguments", void by block |
|||
answer("Not supported in EMal.") |
|||
end) |
|||
task("Using a function in statement context", void by block |
|||
answer("Is supported.") |
|||
if true do noArgumentsFunction() |
|||
else do fixedArgumentsFunction(97, 3.14) end |
|||
end) |
|||
task("Using a function in first-class context within an expression", void by block |
|||
answer("Functions are first class, can be passed as arguments and returned.") |
|||
answer(funArgumentFunction(noArgumentsFunction, 3.14)) |
|||
end) |
|||
task("Obtaining the return value of a function", void by block |
|||
answer("Is supported.") |
|||
int value = noArgumentsFunction() |
|||
answer(value) |
|||
end) |
|||
task("Distinguishing built-in functions and user-defined functions", void by block |
|||
answer("No distinction.") |
|||
end) |
|||
task("Distinguishing subroutines and functions", void by block |
|||
answer("No distinction, we support void return type.") |
|||
end) |
|||
task("Stating whether arguments are passed by value or by reference", void by block |
|||
answer("Pass by value, but text, blob, objects hold a reference.") |
|||
end) |
|||
task("Is partial application possible and how", void by block |
|||
answer("Is supported.") |
|||
^|I had some confusion about partial application and currying, thanks to these links: |
|||
| https://stackoverflow.com/questions/218025/what-is-the-difference-between-currying-and-partial-application |
|||
| https://web.archive.org/web/20161023205431/http://www.uncarved.com/articles/not_curryin |
|||
|^ |
|||
# Partial applying |
|||
fun add = int by int a, int b do return a + b end |
|||
fun partial = fun by fun f, int a |
|||
return int by int b |
|||
return add(a, b) |
|||
end |
|||
end |
|||
fun add7 = partial(add, 7) |
|||
answer(add(7, 5)) |
|||
answer(add7(5)) |
|||
# Currying |
|||
fun addN = fun by int n |
|||
return int by int x |
|||
return x + n |
|||
end |
|||
end |
|||
fun plus = int by int a, int b |
|||
fun addA = addN(a) |
|||
return addA(b) |
|||
end |
|||
answer(plus(7, 5)) |
|||
end) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
· Calling a function that requires no arguments |
|||
Is supported. |
|||
· Calling a function with a fixed number of arguments |
|||
Is supported. |
|||
· Calling a function with optional arguments |
|||
Not supported in EMal. |
|||
· Calling a function with a variable number of arguments |
|||
Variadic functions are supported. |
|||
· Calling a function with named arguments |
|||
Not supported in EMal. |
|||
· Using a function in statement context |
|||
Is supported. |
|||
· Using a function in first-class context within an expression |
|||
Functions are first class, can be passed as arguments and returned. |
|||
100.14 |
|||
· Obtaining the return value of a function |
|||
Is supported. |
|||
97 |
|||
· Distinguishing built-in functions and user-defined functions |
|||
No distinction. |
|||
· Distinguishing subroutines and functions |
|||
No distinction, we support void return type. |
|||
· Stating whether arguments are passed by value or by reference |
|||
Pass by value, but text, blob, objects hold a reference. |
|||
· Is partial application possible and how |
|||
Is supported. |
|||
12 |
|||
12 |
|||
12 |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
<syntaxhighlight lang="erlang"> |