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 anAtom(#tor#f).true_branch: The expression to evaluate ifconditionis#t.false_branch: The expression to evaluate ifconditionis#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: AListor aString.n: ANumberrepresenting the 1-based index.
Return Value
- If
containeris aList: Returns the element at positionn. - If
containeris aString: Returns a single-characterStringat positionn.
Errors
An error will occur if:
nis not an integer or is less than 1.nexceeds the length of thecontainer.
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: AStringto 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 targetList.index: ANumberspecifying the insertion position.- 1-based indexing: A positive index
ninserts theelementbefore the item currently at positionn. - Start: Using
1inserts 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-1is equivalent to appending the element.
- 1-based indexing: A positive index
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
#tifelementis found inlist. - Returns
#fotherwise.
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.