键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

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: A List or a String.

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 first Number.
  • y: The second Number.

Return Value

Returns an Atom.

  • #t if x is strictly less than y.
  • #f if x is greater than or equal to y.

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: A Symbol or a List of 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:

  • target is not a Symbol or a valid list of symbols.
  • The structure of target does not match the structure of value.

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: A Number. 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: The Symbol to which the loaded module object will be bound.
  • module_path: A String representing the path to the module file.

Resolution Logic

  1. Extension: The .ksl extension is omitted in the module_path.
  2. Search Paths: The interpreter searches for the file <module_path>.ksl in:
    • The current working directory.
    • The directory specified by the KSL_PATH environment 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: The String to 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" *)