键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

If

Evaluates a conditional expression and executes one of two branches based on the result.

This function uses short-circuiting. Only the branch corresponding to the condition’s result is evaluated; the other branch is ignored.

Parameters

If[condition, true_branch, false_branch]

The function requires exactly three arguments.

  • condition: An expression that must evaluate to an Atom (#t or #f).
  • true_branch: The expression to evaluate if condition is #t.
  • false_branch: The expression to evaluate if condition is #f.

Return Value

Returns the result of the evaluated branch.

Errors

An error will occur if the condition does not evaluate to a boolean #t or #f.

Examples

(* Basic condition *)
If[#t, 1, 2]; (* 1 *)
If[#f, 1, 2]; (* 2 *)

(* Demonstrating lazy evaluation
  Because the condition is #t, the second branch is never executed *)
If[#t, #safe, Div[1, 0]]; (* #safe *)

Index

Retrieves the element at a specific position within a list or a string. KSL uses 1-based indexing.

Parameters

Index[container, n]

The function requires exactly two arguments.

  • container: A List or a String.
  • n: A Number representing the 1-based index.

Return Value

  • If container is a List: Returns the element at position n.
  • If container is a String: Returns a single-character String at position n.

Errors

An error will occur if:

  • n is not an integer or is less than 1.
  • n exceeds the length of the container.

Examples

(* Indexing a List *)
Index[{ 1, 2, 3, 4 }, 3]; (* 3 *)

(* Indexing a String (Unicode aware) *)
Index["你好👋,世界🌍,World is Beautiful!", 5]; (* "世" *)

(* Out of bounds error *)
Index[{ 1 }, 2];
(* ?
Error[ksl::builtin::Index]: Index `2` out of bounds `1`.
*)

Input

Reads a line of text from the standard input (stdin).

Parameters

Input[prompt]

The function accepts zero or one argument.

  • prompt: A String to display to the user before waiting for input. If omitted, the default prompt > is used.

Return Value

Returns a String containing the text entered by the user, excluding the final newline character.

Errors

An error will occur if the prompt is not a String.

Examples

(* Default prompt *)
Input[];
(* => > test *)
(* "test" *)

(* Custom prompt *)
Input["Enter name:"];
(* => Enter name: Alice *)
(* "Alice" *)

Insert

Returns a new list with a given element inserted at a specific position.

Parameters

Insert[list, index, element]

The function requires exactly three arguments.

  • list: The target List.
  • index: A Number specifying the insertion position.
    • 1-based indexing: A positive index n inserts the element before the item currently at position n.
    • Start: Using 1 inserts at the very beginning.
    • End: Using Add[Length[list], 1] inserts at the very end.
    • Special Value -1: Represents the end of the list. Using -1 is equivalent to appending the element.
  • element: The value to be inserted.

Return Value

Returns a new List with the element inserted. The original list remains unchanged.

Errors

An error will occur if the index is out of valid bounds.

Examples

Let[v, { 1, 2, 3, 4 }];

(* Insert at the beginning (Index 1) *)
Insert[v, 1, #elem]; (* {#elem, 1, 2, 3, 4} *)

(* Insert in the middle (Index 3) *)
(* The element is placed before the original 3rd element (which was 3) *)
Insert[v, 3, #elem]; (* {1, 2, #elem, 3, 4} *)

(* Insert at the end using precise indexing (Index 5) *)
Insert[v, 5, #elem]; (* {1, 2, 3, 4, #elem} *)

(* Insert at the end using the special index -1 *)
Insert[v, -1, #elem]; (* {1, 2, 3, 4, #elem} *)

Type Predicates

The following functions are used to check the type or properties of a value. They all return an Atom (#t or #f) and do not throw domain-specific errors.

IsAtom

IsAtom[x]

Returns #t if x is an Atom, otherwise #f.

IsBuiltin

IsBuiltin[x]

Returns #t if x is a built-in function, otherwise #f.

IsInteger

IsInteger[x]

Returns #t if x is a Number and represents a whole number, otherwise #f.

IsInteger[10];   (* #t *)
IsInteger[10.0]; (* #t *)
IsInteger[10.5]; (* #f *)
IsInteger["1"];  (* #f *)

IsLambda

IsLambda[x]

Returns #t if x is an anonymous function, otherwise #f.

IsList

IsList[x]

Returns #t if x is a List, otherwise #f.

IsMember

IsMember[list, element]

Checks if a specific element exists within a list. Note that this function takes two arguments.

  • Returns #t if element is found in list.
  • Returns #f otherwise.
IsMember[{ 1, 2, 3 }, 2]; (* #t *)
IsMember[{ 1, 2, 3 }, 5]; (* #f *)

IsNativeObject

IsNativeObject[x]

Returns #t if x is a NativeObject, otherwise #f.

IsNumber

IsNumber[x]

Returns #t if x is a Number, otherwise #f.

IsObject

IsObject[x]

Returns #t if x is a standard Object, otherwise #f.

IsPlugin

IsPlugin[x]

Returns #t if x is a plugin function loaded from an external dynamic library, otherwise #f.

IsString

IsString[x]

Returns #t if x is a String, otherwise #f.

IsThread

IsThread[x]

Returns #t if x is a Thread handle, otherwise #f.

IsUnit

IsUnit[x]

Returns #t if x is the Unit value Unit[], otherwise #f.