键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Ceiling

Computes the ceiling of a number, which is the smallest integer greater than or equal to that number.

Parameters

Ceiling[n]

The function requires exactly one argument.

  • n: The Number to be rounded up.

Return Value

Returns a Number representing the smallest integer value that is greater than or equal to n.

No domain-specific errors are thrown by this function.

Examples

(* Rounds a positive number up *)
Ceiling[5.3]; (* 6 *)

(* Rounds a negative number up (towards positive infinity) *)
Ceiling[-5.8]; (* -5 *)

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

(* The ceiling of zero is zero *)
Ceiling[0]; (* 0 *)

Chars

Splits a string into a list of its constituent characters, returning each character as a single-character string.

This function is fully Unicode-aware and correctly handles multi-byte characters and complex graphemes (like emoji) as single units. KSL does not have a distinct character data type; instead, each “character” is represented as a String. Special characters, such as newlines, are also converted into their own single-character string elements.

Parameters

Chars[s]

The function requires exactly one argument.

  • s: The String to be split into characters.

Return Value

Returns a List of Strings. Each string in the list corresponds to a single character from the input string, in the order they appeared.

No domain-specific errors are thrown by this function.

Examples

(* Splits a simple ASCII string *)
Chars["abc"]; (* {"a", "b", "c"} *)

(* Correctly handles multi-byte Unicode characters, newlines, and emoji *)
Chars["你好👋,世界🌏!"];
(* {"你", "好", "👋", ",", "世", "界", "🌏", "!"} *)

Chars["你好
世界
🌍"];
(* {"你", "好", "\n", "世", "界", "\n", "🌍"} *)

Note: The "\n" in the output represents the single newline character. In KSL source code, this character is created with a literal newline (as shown above) or by using its Unicode code point, #10. The string literal "\n" would be interpreted as a two-character string: "\" and "n".

Chn

Converts between a numeric Unicode code point and its corresponding string character.

This function acts as a bidirectional converter: it turns an integer code point into a character string, or a single-character string into its integer code point.

Parameters

Chn[value]

The function requires exactly one argument. The behavior depends on the type of the argument.

  • value:
    • If it is a Number, it is interpreted as a decimal Unicode code point.
    • If it is a String, it must contain exactly one Unicode character.

Return Value

  • Number: Returns a single-character String corresponding to the provided code point.
  • String: Returns a Number representing the decimal Unicode code point of the character.

Errors

An error will occur under the following conditions:

  • If the input Number is not a valid Unicode scalar value.
  • If the input String does not contain exactly one valid character.

Examples

Character to Number

(* Converts a Chinese character to its code point *)
Chn["你"]; (* 20320 *)

(* Converts a standard ASCII character *)
Chn["a"]; (* 97 *)

(* This will cause an error because the emoji sequence is not treated as a single scalar value *)
Chn["❤️"];
(* ?
Error[ksl::builtin::Chn]: Invalid single character: `❤️`.
*)

Number to Character

(* Converts a code point back to a character *)
Chn[97]; (* "a" *)

(* Converts a code point to a CJK Unified Ideograph *)
Chn[19198]; (* "䫾" *)

(* This will cause an error because the number is outside the valid Unicode range *)
Chn[1919812];
(* ?
Error[ksl::builtin::Chn]: Invalid unicode: `1919812`.
*)

CloseStream

Closes an active UDP Socket, releasing its underlying system resources.

Parameters

CloseStream[symbol]

The function requires exactly one argument.

  • symbol: The argument must be a Symbol that is bound to an active UDP Socket object.

    A UDP Socket is a NativeObject with the type tag #UDPSocket.

Return Value

Returns Unit[].

As a crucial side effect, this function rebinds the input symbol itself to Unit[]. This destructive action prevents dangling references, making it safe by ensuring the closed UDP Socket cannot be used again.

No domain-specific errors are thrown by this function.

Examples

(* create a UDP socket and binds it to s *)
OpenStream[s, "127.0.0.1:11453"];

(* Verify the type and value of s *)
GetType[s]; (* #UDPSocket *)
Print[s]; (* => Native[UDPSocket] *)

(* Close the UDP Socket *)
CloseStream[s]; (* () *)

(* After closing, the variable s itself now holds Unit[] *)
Print[s]; (* => () *)

Concat

Concatenates a sequence of strings into a single string, or a sequence of lists into a single list.

Parameters

Concat[item1, item2, ...]

The function requires at least one argument.

  • item1, item2, ...: A sequence of arguments that must be either all Strings or all Lists.

Return Value

  • If all arguments are Strings, returns a new String representing their concatenation.
  • If all arguments are Lists, returns a new List containing the elements of all input lists in order. The concatenation is shallow; nested lists are preserved and not flattened.

No domain-specific errors are thrown by this function.

Examples

String Concatenation

Print[Concat["hello,", #10, "world!"]];
(* =>
hello,
world!
*)

List Concatenation

Concat[{ 1 }, { 2, 3 }, { 4, { 5, 6 } }, { { 7 } }];
(* {1, 2, 3, 4, {5, 6}, {7}} *)

Error on Missing Arguments

Concat[];
(* ?
Error[ksl::builtin::Concat]: Expected at least 1 parameter, but 0 were passed.
*)

Cond

Evaluates a sequence of condition-result pairs and returns the result corresponding to the first condition that evaluates to #t.

This function serves as a cleaner alternative to deeply nested If expressions when there are multiple conditions to check.

Parameters

Cond[branches, fallback]

The function requires exactly two arguments.

  • branches: A List of pairs.

    Each pair must itself be a List containing exactly two elements: { condition, result }.

    • condition: An expression that evaluates to a boolean Atom (#t or #f).
    • result: The value to return if the condition is #t.
  • fallback: The value to return if none of the conditions in branches evaluate to #t.

Evaluation Logic

  1. The function iterates through the branches list in order.
  2. For each { condition, result } pair, the condition is evaluated.
  3. Short-Circuiting:
    • If a condition evaluates to #t, the function immediately evaluates and returns the corresponding result. Subsequent conditions are skipped.
    • If a condition evaluates to #f, the function proceeds to the next pair.
  4. If the end of the branches list is reached without any condition being #t, the fallback is evaluated and returned.

Return Value

Returns the result of the first matching branch, or the default_value.

Errors

An error will occur if:

  • branches is not a List.
  • Any element within branches is not a List of two elements.
  • A condition expression does not evaluate to a boolean.

Examples

Let[x, 8];
Cond[{
  { Less[x, 6], #low },
  { Less[x, 8], #mid }
}, #high]; (* #high *)

Let[x, 3];
Cond[{
  { Less[x, 6], #low },
  { Less[x, 8], #mid }
}, #high]; (* #low *)

Consume

Waits for a thread to complete its execution and retrieves its return value.

This function “consumes” the thread handle, meaning it can only be successfully called once for any given thread. It blocks the current execution flow until the target thread has finished.

Parameters

Consume[thread]

The function requires exactly one argument.

  • thread: A Thread object, created by the Thread function.

Return Value

The return value depends on whether the thread has been previously consumed.

  • On the first successful call, it returns the value that was returned by the expression evaluated within the thread. The type of this value is determined by the thread’s final expression.

  • If the thread has already been consumed, the function returns a List indicating an error:

    {#err, "Removed thread."}
    

No domain-specific errors are thrown by this function.

Examples

The following example demonstrates creating two threads, consuming them, and then attempting to consume one a second time.

Block[
  Thread[t1, { }, Block[
    Sleep[1],
    Print["Finished 1"],
    1
  ]],
  Thread[t2, { }, Block[
    Sleep[0.5],
    Print["Finished 2"],
    2
  ]],
  Print[Consume[t1]],
  Print[Consume[t2]],
  Print[Consume[t1]]
];
(* =>
Finished 2
Finished 1
1
2
{#err, "Removed thread."}
*)

Explanation of the output:

  • Finished 2 appears first because t2 has a shorter sleep duration.

  • Finished 1 appears next. The main thread was blocked by Consume[t1] until t1 completed.

  • 1 is the return value of Consume[t1].

  • 2 is the return value of Consume[t2].

    The main thread retrieves this value instantly as t2 had already finished.

  • {#err, "Removed thread."} is returned by the second attempt to consume t1.

Cos

Computes the cosine of an angle given in radians.

Parameters

Cos[n]

The function requires exactly one argument.

  • n: A Number representing the angle in radians.

Return Value

Returns a Number in the range $\left[-1, 1\right]$, which is the cosine of the input angle.

No domain-specific errors are thrown by this function.

Examples

(* The cosine of 0 is 1 *)
Cos[0]; (* 1 *)

(* The cosine of pi is -1 *)
Cos[3.1415926535898]; (* -1 *)

(* The cosine of pi/2 is almost 0 *)
Cos[1.5707963267949]; (* 6.12323399573677e-17 *)

Cos[-1]; (* 0.54030230586814 *)

Cosh

Computes the hyperbolic cosine of a number.

Parameters

Cosh[n]

The function requires exactly one argument.

  • n: A Number.

Return Value

Returns a Number greater than or equal to 1, which is the hyperbolic cosine of the input.

No domain-specific errors are thrown by this function.

Examples

(* The hyperbolic cosine of 0 is 1 *)
Cosh[0]; (* 1 *)

(* Cosh is an even function, so Cosh[x] == Cosh[-x] *)
Cosh[1]; (* 1.54308063481524 *)
Cosh[-1]; (* 1.54308063481524 *)
Eq[Cosh[1], Cosh[-1]]; (* #t *)