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
conditiondoes not evaluate to#tor#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 positionn. - For a
String: returns a single-characterStringat positionn(Unicode-aware). - An error is raised if
nis 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
promptis provided, it is displayed before waiting for input; otherwise a default>prompt is used. - Returns a
Stringcontaining 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
ninserts before the current element at positionn. - Index
-1is a special value that inserts at the end of the list. - An error is raised if
indexis 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
#tif 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.
| Function | Returns #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 *)