键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Eq

Performs a deep equality comparison between any two KSL values.

Parameters

Eq[value1, value2]

The function requires exactly two arguments.

  • value1, value2: The two values to be compared. They can be of any type.

Return Value

Returns the #t if the values are equal, and #f otherwise.

The comparison logic is strictly defined as follows:

  • Primitive Types: Unit, Symbol, Atom, String, and Number values are equal if their underlying values are identical.

  • Lists: Two Lists are equal only if they have the same length and each corresponding element is also equal.

  • Objects: Two Objects are equal only if they have the same type tag and contain the exact same set of key-value pairs.

    The order of keys does not affect the comparison.

  • Otherwise, it will return #f.

No domain-specific errors are thrown by this function.

Examples

(* Number equality *)
Eq[2, 2.0]; (* #t *)

(* Atom equality *)
Eq[#ok, #ok]; (* #t *)
Eq[#t, #f]; (* #f *)

(* List deep equality *)
Eq[{ 1, { 2, 3 } }, { 1, { 2, 3 } }]; (* #t *)
Eq[{ 1, 2 }, { 2, 1 }]; (* #f *)

(* Object equality (key order does not matter) *)
Let[obj1, Object[T, {{#a, 1}, {#b, 2}}]];
Let[obj2, Object[T, {{#b, 2}, {#a, 1}}]];
Eq[obj1, obj2]; (* #t *)

(* Mixed type comparison *)
Eq[1, "1"]; (* #f *)

Evial

Executes KSL source code from a string within the current evaluation environment. This function is analogous to the eval function found in many other programming languages.

Parameters

Evial[code]

The function requires exactly one argument.

  • code: A String containing a valid KSL program.

Return Value

Returns the result of the last expression evaluated in the code. The type of this value is determined by that final expression.

Crucially, any side effects from the executed code, such as variable definitions or modifications with Let, will persist in the current scope after Evial returns.

Errors

An error will be thrown if the input string contains invalid code.

Examples

Modifying the Environment

This example shows how Evial can be used to define a new variable in the current scope.

(* The symbol 'a' is not yet defined *)
Has[a]; (* #f *)

(* Evial executes the Let expression, which binds the value 10 to 'a' *)
Evial["Let[a, 10];"]; (* () *)

(* Now the symbol 'a' exists in the current environment *)
Has[a]; (* #t *)
Print[a]; (* => 10 *)

Returning a Value

The function returns the value of the last expression in the string.

Add[Evial["Add[5, 5]"], 6]; (* 16 *)

Exp

Computes the value of the natural exponential function ($\mathrm{\mathbf{e}}^x$).

Parameters

Exp[n]

The function requires exactly one argument.

  • n: A Number representing the exponent.

Return Value

Returns a Number which is the result of the calculation. In accordance with f64 floating-point standards, the result can be inf if the input n is very large.

No domain-specific errors are thrown by this function.

Examples

(* e^1 is the mathematical constant e *)
Exp[1]; (* 2.71828182845905 *)

(* e^0 is 1 *)
Exp[0]; (* 1 *)

(* e raised to a negative power *)
Exp[-1]; (* 0.367879441171442 *)

(* A large input can result in infinity *)
Exp[1000]; (* inf *)