键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

If

If[condition, true_branch, false_branch]

Evaluates condition and executes true_branch if it is #t, otherwise executes false_branch. Short-circuiting ensures only the selected branch is evaluated.

  • Returns the result of the evaluated branch.
  • An error is raised if condition does not evaluate to #t or #f.
If[#t, 1, 2];         (* => 1 *)
If[#f, 1, 2];         (* => 2 *)
If[#t, #safe, Div[1, 0]];  (* => #safe *)

Index

Index[container, n]

Retrieves the element at 1-based index n from a list or string.

  • For a List: returns the element at position n.
  • For a String: returns a single-character String at position n (Unicode-aware).
  • An error is raised if n is not a positive integer or exceeds the container’s length.
Index[{ 1, 2, 3, 4 }, 3];                        (* => 3 *)
Index["你好👋,世界🌍,World is Beautiful!", 5]; (* => "世" *)

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

Input

Input[] Input[prompt]

Reads a line of text from standard input.

  • If prompt is provided, it is displayed before waiting for input; otherwise a default > prompt is used.
  • Returns a String containing the entered line, excluding the trailing newline.
Input[];
(*
> test
=> "test"
*)
Input["Enter name:"];  (* Enter name: Alice => "Alice" *)

Insert

Insert[list, index, element]

Returns a new list with element inserted at the specified 1-based index. The original list is unchanged.

  • A positive index n inserts before the current element at position n.
  • Index -1 is a special value that inserts at the end of the list.
  • An error is raised if index is out of valid bounds.
Let[v, { 1, 2, 3, 4 }];

Insert[v, 1, #elem];   (* => {#elem, 1, 2, 3, 4} *)
Insert[v, 3, #elem];   (* => {1, 2, #elem, 3, 4} *)
Insert[v, 5, #elem];   (* => {1, 2, 3, 4, #elem} *)
Insert[v, -1, #elem];  (* => {1, 2, 3, 4, #elem} *)

IsMember

IsMember[list, element]

Checks whether element is present in list.

  • Returns #t if found, otherwise #f.
IsMember[{ 1, 2, 3 }, 2];  (* => #t *)
IsMember[{ 1, 2, 3 }, 5];  (* => #f *)

Type Predicates

The following functions test the type of a single value x. Each accepts exactly one argument, returns #t or #f, and never throws a domain-specific error.

FunctionReturns #t if x is…
IsAtom[x]An Atom (e.g., #t, #ok, #my'tag).
IsBuiltin[x]A built-in KSL function.
IsInteger[x]A Number that represents a whole number without a fractional part.
IsLambda[x]An anonymous function (created via Fun).
IsList[x]A List.
IsNativeObject[x]A NativeObject (e.g., a channel sender/receiver).
IsNumber[x]A Number.
IsObject[x]A standard KSL Object.
IsPlugin[x]A plugin function loaded from an external dynamic library.
IsString[x]A String.
IsThread[x]A Thread handle.
IsUnit[x]The Unit value (Unit[]).
IsInteger[10];    (* => #t *)
IsInteger[10.0];  (* => #t *)
IsInteger[10.5];  (* => #f *)

IsString["hello"];  (* => #t *)
IsList[{ 1, 2 }];   (* => #t *)
IsAtom[#t];         (* => #t *)