Length
Computes the number of elements in a list or the number of characters in a string.
Parameters
Length[container]
The function requires exactly one argument.
container: AListor aString.
Return Value
Returns a Number.
- For Lists: The count of elements.
- For Strings: The count of Unicode characters. This function is UTF-8 aware.
- Note: Some complex graphemes are composed of multiple scalar values and will contribute their scalar count to the length, not necessarily 1.
Errors
An error will occur if the argument is not a List or a String.
Examples
(* List length *)
Length[{ 1, 2, 3 }]; (* 3 *)
(* String length (UTF-8) *)
(* The flag 🇺🇸 consists of two region indicator scalars *)
Length["👋你好🇺🇸美国"]; (* 7 *)
Less
Compares two numbers and returns #t if the first is strictly less than the second.
Parameters
Less[x, y]
The function requires exactly two arguments.
x: The firstNumber.y: The secondNumber.
Return Value
Returns an Atom.
#tifxis strictly less thany.#fifxis greater than or equal toy.
Errors
An error will occur if either argument is not a Number.
Examples
Less[5, 10]; (* #t *)
Less[10, 5]; (* #f *)
Less[5, 5]; (* #f *)
Let
Binds a value to a symbol in the current environment. If the symbol is already defined, its value is shadowed.
Parameters
Let[target, value]
The function requires exactly two arguments.
target: ASymbolor aListof symbols for destructuring.value: The value to bind.
Destructuring
Let supports a concise syntax for assigning values to multiple symbols simultaneously using lists.
- Constraint: Only one level of nesting is supported for matching. Deeply nested destructuring is not available.
Return Value
Returns Unit[].
The primary purpose of this function is its side effect.
Errors
An error will occur if:
targetis not aSymbolor a valid list of symbols.- The structure of
targetdoes not match the structure ofvalue.
Examples
Basic Binding
Let[a, 10];
Print[a]; (* => 10 *)
(* Shadowing *)
Let[a, 20];
Print[a]; (* => 20 *)
Destructuring
(* Binds a=12 and b=34 simultaneously *)
Let[{ a, b }, { 12, 34 }];
Ln
Computes the natural logarithm (base $e$) of a number.
Parameters
Ln[n]
The function requires exactly one argument.
n: ANumber. It must be strictly positive.
Return Value
Returns a Number representing the natural logarithm of n.
Errors
The evaluation of Ln will fail if the input n is less than or equal to zero.
Examples
Ln[1]; (* 0 *)
Ln[2.3]; (* 0.832909122935104 *)
(* Logarithm of zero is undefined in KSL *)
Ln[0];
(* ?
Error[ksl::builtin::Ln]: Logarithm of non-positive number.
*)
(* Logarithm of a negative number is undefined in KSL *)
Ln[-1];
(* ?
Error[ksl::builtin::Ln]: Logarithm of non-positive number.
*)
Load
Loads a KSL module from a file and binds it to a symbol.
Parameters
Load[symbol, module_path]
The function requires exactly two arguments.
symbol: TheSymbolto which the loaded module object will be bound.module_path: AStringrepresenting the path to the module file.
Resolution Logic
- Extension: The
.kslextension is omitted in themodule_path. - Search Paths: The interpreter searches for the file
<module_path>.kslin:- The current working directory.
- The directory specified by the
KSL_PATHenvironment variable (defaults to~/.local/share/ksl).
Return Value
Returns Unit[]. The module object is bound to symbol in the current scope.
Errors
An error will occur if the module file cannot be found or if the module code contains syntax errors.
Examples
(* Loads ~/.local/share/ksl/std/string.ksl and binds it to 'ss' *)
Load[ss, "std/string"];
Print[ss];
(* =>
Module[std/string]{Backspace, VerticalTabulation, JoinWith, Capitalize, MODULE_NAME, DoubleQuote, NewLine, HorizontalTab, Alert, Acknowledge}
*)
(* Accessing a function from the loaded module *)
Use[ss, Capitalize];
(* => λ(s) => <lambda> *)
Lowercase
Converts all characters in a string to lowercase.
Parameters
Lowercase[s]
The function requires exactly one argument.
s: TheStringto convert.
Return Value
Returns a new String with all characters converted to their lowercase equivalents.
Errors
An error will occur if the argument is not a String.
Examples
Lowercase["Hello World"]; (* "hello world" *)
Lowercase["KSL"]; (* "ksl" *)