41 lines
1.9 KiB
Text
41 lines
1.9 KiB
Text
Naming conventions
|
|
|
|
1) global names
|
|
|
|
- a name is a group of any character except space and curly braces {}
|
|
for instance "add", "+", "123", "()", "a.b.c", "...", ...
|
|
|
|
- but giving a variable the name of the 9 special forms (keywords)
|
|
["lambda", "def", "if", "let", "quote", "macro", "script", "style", "require"]
|
|
or of one of the more than 170 primitives
|
|
["div", ..., "+", ..., "cons", ..., A.new, A.first, A.rest, ..., long_add, ...]
|
|
is possible but is obviously strongly discouraged.
|
|
|
|
- when a function calls an helper function, it's better to prefix the helper function's name
|
|
with the calling function's name. For instance the function "fibonacci" sets
|
|
some initializations and calls the recursive part "fibonacci.rec".
|
|
|
|
- functions organized into sets (libraries) should be named with a common prefix.
|
|
For instance the set of functions working on complex numbers [ "C.new", "C.x", "C.y", "C.+", "C.*", ...],
|
|
suggesting object-oriented programming.
|
|
|
|
2) local names
|
|
|
|
Consider this example:
|
|
|
|
{def add {lambda {:a :b} :a+:b is equal to {+ :a :b}}}
|
|
-> add
|
|
{add 3 4}
|
|
-> 3+4 is equal to 7
|
|
|
|
When the add function is called the occurences of arguments {:a :b} are replaced by the given values, [3 4],
|
|
in the body of the function ":a+:b is equal to {+ :a :b}", leading to "3+4 is equal to {+ 3 4}" and finally
|
|
to "3+4 is equal to 7". Note that the character "a" inside the word "equal" has not been replaced.
|
|
So generally speaking:
|
|
|
|
- inside a function local defined arguments must be prefixed by a colon, ":",
|
|
- they must have a null intersection. For instance this arguments list {:a1 :a2} is valid
|
|
because the intersection of :a1 and :a2 is null, but this one {:a :a1} is not,
|
|
because the intersection of :a and :a1 is :a.
|
|
- an alternative could be to prefix and postfix names, say :a: and :a1: which have a null intersection.
|
|
It's a matter of choice let to the coder. In all cases naming must be done with the utmost care.
|