键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Filter

A higher-order function that creates a new list containing only the elements from an input list for which a predicate function returns #t.

Parameters

Filter[list, predicate]

The function requires exactly two arguments.

  • list: The List to be filtered.

  • predicate: A function or a lambda that serves as the condition.

    This function must:

    • Accept a single argument. It must be able to operate on the element types present in the list.
    • Return either the atom #t or #f.

Return Value

Returns a new List.

  • The new list contains all elements from the original list for which the predicate returned #t.
  • The relative order of the elements is preserved.
  • If no elements satisfy the predicate, or if the input list is empty, an empty list is returned.

Errors

The evaluation of Filter will fail and propagate an error if the predicate function fails when applied to any element. This typically occurs due to a type mismatch, where an element in the list is not a valid argument for the operation inside the predicate.

Examples

Let[numbers, { 1, 2, 3, 4, 5, 6 }];

(* Filters for all even numbers *)
Filter[numbers, Fun[{ x }, Eq[Mod[x, 2], 0]]]; (* {2, 4, 6} *)

(* Filters for all numbers greater than 3 *)
Filter[numbers, Fun[{ x }, Greater[x, 3]]]; (* {4, 5, 6} *)

(* Filtering an empty list results in an empty list *)
Filter[{ }, Fun[{ x }, Eq[x, 1]]]; (* {} *)

(* This will cause an error *)
Let[mixed_list, { 1, "two", 3 }];
(* The call to Greater will fail for the element "two",
  causing Filter to propagate the error. *)
Filter[mixed_list, Fun[{ x }, Greater[x, 0]]];
(* ?
Error[ksl::builtin::Greater]: Unexpected value: `"two"`.
*)

Find

Searches for an element in a list and returns its index.

The function finds the first occurrence of the element.

Parameters

Find[list, element]

The function requires exactly two arguments.

  • list: The List to be searched.
  • element: The value to find within the list.

Return Value

Returns a Number.

  • If the element is found, the function returns its 1-based index.
  • If the element is not found in the list, the function returns 0.

No domain-specific errors are thrown by this function.

Examples

Let[items, { 10, #twenty, { 30 }, #twenty }];

(* Finds the first occurrence of the atom #twenty at the 2nd position *)
Find[items, #twenty]; (* 2 *)

(* The nested list { 30 } is found at the 3rd position *)
Find[items, { 30 }]; (* 3 *)

(* Returns 0 because the number 40 is not in the list *)
Find[items, 40]; (* 0 *)

(* Searching in an empty list always returns 0 *)
Find[{ }, 1]; (* 0 *)

Floor

Computes the floor of a number, which is the largest integer less than or equal to that number.

Parameters

Floor[n]

The function requires exactly one argument.

  • n: The Number to be rounded down.

Return Value

Returns a Number representing the largest integer value that is less than or equal to n.

No domain-specific errors are thrown by this function.

Examples

(* Rounds a positive number down *)
Floor[5.8]; (* 5 *)

(* Rounds a negative number down *)
Floor[-5.3]; (* -6 *)

(* An integer input remains unchanged *)
Floor[4]; (* 4 *)

(* The floor of zero is zero *)
Floor[0]; (* 0 *)

Fun

Defines a first-class anonymous function, commonly known as a lambda or closure.

This is the primary mechanism in KSL for creating functions as values. A lambda created with Fun captures a snapshot of the lexical environment at the time of its definition, allowing it to access variables from its surrounding scope.

Parameters

Fun[parameters, body]

The function requires exactly two arguments.

  • parameters: A List of Symbols that define the formal arguments for the lambda.

    An empty list { } indicates the function takes no arguments.

  • body: A single expression that forms the function’s body.

    This expression is only evaluated when the lambda is invoked.

Return Value

Returns a Lambda object.

This value can be stored in variables, passed as an argument to other higher-order functions, or returned from other functions.

No domain-specific errors are thrown by this function.

Examples

Basic Lambda Definition

(* Defines a function `add` that adds two numbers *)
Let[add, Fun[{ x, y }, Add[x, y]]];

(* Call function *)
add[5, 10]; (* 15 *)

(* Invokes the lambda immediately *)
Fun[{ x }, Fun[{ y }, Add[x, y]]][10][20]; (* 30 *)

Demonstrating Scope Isolation

This example shows that modifying a captured variable inside a lambda does not affect the original variable.

Let[a, 0];

Let[tryAdd, Fun[{ }, Let[a, Add[a, 1]]]];
tryAdd[];

(* The original `a` remains unchanged *)
Print[Eq[a, 0]]; (* => #t *)