This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View 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

View 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

View 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.

View 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.

View 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));

View file

@ -0,0 +1 @@
write("Nr: " <& num); # Use operators to concatenate arguments.

View file

@ -0,0 +1 @@
ignore(getln(IN)); # Using a function in statement context (ignore the result).

View file

@ -0,0 +1 @@
seq := doMap([](1, 2, 4, 6, 10, 12, 16), x, succ(x));