update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
32
Task/Call-a-function/Lua/call-a-function.lua
Normal file
32
Task/Call-a-function/Lua/call-a-function.lua
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
-- Lua functions accept any number of arguments; missing arguments are nil-padded, extras are dropped.
|
||||
function fixed (a, b, c) print(a, b, c) end
|
||||
fixed() --> nil nil nil
|
||||
fixed(1, 2, 3, 4, 5) --> 1 2 3
|
||||
|
||||
-- True vararg functions include a trailing ... parameter, which captures all additional arguments as a group of values.
|
||||
function vararg (...) print(...) end
|
||||
vararg(1, 2, 3, 4, 5) -- 1 2 3 4 5
|
||||
|
||||
-- Lua also allows dropping the parentheses if table or string literals are used as the sole argument
|
||||
print "some string"
|
||||
print { foo = "bar" } -- also serves as a form of named arguments
|
||||
|
||||
-- First-class functions in expression context
|
||||
print(("this is backwards uppercase"):gsub("%w+", function (s) return s:upper():reverse() end))
|
||||
|
||||
-- Functions can return multiple values (including none), which can be counted via select()
|
||||
local iter, obj, start = ipairs { 1, 2, 3 }
|
||||
print(select("#", (function () end)())) --> 0
|
||||
print(select("#", unpack { 1, 2, 3, 4 })) --> 4
|
||||
|
||||
-- Partial application
|
||||
function prefix (pre)
|
||||
return function (suf) return pre .. suf end
|
||||
end
|
||||
|
||||
local prefixed = prefix "foo"
|
||||
print(prefixed "bar", prefixed "baz", prefixed "quux")
|
||||
|
||||
-- nil, booleans, and numbers are always passed by value. Everything else is always passed by reference.
|
||||
-- There is no separate notion of subroutines
|
||||
-- Built-in functions are not easily distinguishable from user-defined functions
|
||||
38
Task/Call-a-function/Racket/call-a-function.rkt
Normal file
38
Task/Call-a-function/Racket/call-a-function.rkt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#lang racket
|
||||
|
||||
;; Calling a function that requires no arguments
|
||||
(foo)
|
||||
|
||||
;; Calling a function with a fixed number of arguments
|
||||
(foo 1 2 3)
|
||||
|
||||
;; Calling a function with optional arguments
|
||||
;; Calling a function with a variable number of arguments
|
||||
(foo 1 2 3) ; same in both cases
|
||||
|
||||
;; Calling a function with named arguments
|
||||
(foo 1 2 #:x 3) ; using #:keywords for the names
|
||||
|
||||
;; Using a function in statement context
|
||||
;; Using a function in first-class context within an expression
|
||||
;; Obtaining the return value of a function
|
||||
;; -> Makes no sense for Racket, as well as most other functional PLs
|
||||
|
||||
;; Distinguishing built-in functions and user-defined functions
|
||||
(primitive? foo)
|
||||
;; but this is mostly useless, since most of Racket is implemented in
|
||||
;; itself
|
||||
|
||||
;; Distinguishing subroutines and functions
|
||||
;; -> No difference, though `!' is an idiomatic suffix for names of
|
||||
;; side-effect functions, and they usually return (void)
|
||||
|
||||
;; Stating whether arguments are passed by value or by reference
|
||||
|
||||
;; -> Always by value, but it's possible to implement languages with
|
||||
;; other argument passing styles, including passing arguments by
|
||||
;; reference (eg, there is "#lang algol60")
|
||||
|
||||
;; Is partial application possible and how
|
||||
(curry foo 1 2) ; later apply this on 3
|
||||
(λ(x) (foo 1 2 x)) ; a direct way of doing the same
|
||||
3
Task/Call-a-function/Seed7/call-a-function-1.seed7
Normal file
3
Task/Call-a-function/Seed7/call-a-function-1.seed7
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
env := environment; # Call a function that requires no arguments.
|
||||
env := environment(); # Alternative possibility to call of a function with no arguments.
|
||||
cmp := compare(i, j); # Call a function with a fixed number of arguments.
|
||||
2
Task/Call-a-function/Seed7/call-a-function-2.seed7
Normal file
2
Task/Call-a-function/Seed7/call-a-function-2.seed7
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
write(aFile, "asdf"); # Variant of write with a parameter to specify a file.
|
||||
write("asdf"); # Variant of write which writes to the file OUT.
|
||||
13
Task/Call-a-function/Seed7/call-a-function-3.seed7
Normal file
13
Task/Call-a-function/Seed7/call-a-function-3.seed7
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
const func integer: sum (in array integer: intElems) is func
|
||||
result
|
||||
var integer: sum is 0;
|
||||
local
|
||||
var integer: element is 0;
|
||||
begin
|
||||
for element range intElems do
|
||||
sum +:= element;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
s := sum([] (1, 2, 3)); # Use an aggregate to generate an array.
|
||||
t := sum([] (2, 3, 5, 7));
|
||||
1
Task/Call-a-function/Seed7/call-a-function-4.seed7
Normal file
1
Task/Call-a-function/Seed7/call-a-function-4.seed7
Normal file
|
|
@ -0,0 +1 @@
|
|||
write("Nr: " <& num); # Use operators to concatenate arguments.
|
||||
1
Task/Call-a-function/Seed7/call-a-function-5.seed7
Normal file
1
Task/Call-a-function/Seed7/call-a-function-5.seed7
Normal file
|
|
@ -0,0 +1 @@
|
|||
ignore(getln(IN)); # Using a function in statement context (ignore the result).
|
||||
1
Task/Call-a-function/Seed7/call-a-function-6.seed7
Normal file
1
Task/Call-a-function/Seed7/call-a-function-6.seed7
Normal file
|
|
@ -0,0 +1 @@
|
|||
seq := doMap([](1, 2, 4, 6, 10, 12, 16), x, succ(x));
|
||||
Loading…
Add table
Add a link
Reference in a new issue