键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Cd

Cd[path]

Changes the current working directory of the process to the specified path.

  • Returns Unit[].
  • An error is raised if the directory does not exist, is not a directory, or if permissions are insufficient.
Print[Pwd[]];               (* => "/home/user" *)

Cd["projects/ksl"];
Print[Pwd[]];               (* => "/home/user/projects/ksl" *)

Cd[".."];
Print[Pwd[]];               (* => "/home/user/projects" *)

Cd["/non_existent_path"];
(*? Error[ksl::builtin::Cd]: Expected a valid path, but got: `/non_existent_path` with `No such file or directory (os error 2)`. *)

Ceiling

Ceiling[n]

Computes the ceiling of n: the smallest integer greater than or equal to n.

  • Returns a Number.
Ceiling[5.3];   (* => 6 *)
Ceiling[-5.8];  (* => -5 *)
Ceiling[4];     (* => 4 *)
Ceiling[0];     (* => 0 *)

Chars

Chars[s]

Splits a string s into a list of its constituent characters, returning each as a single-character string. The function is fully Unicode-aware and correctly handles multi-byte characters and graphemes as single units.

  • Returns a List of single-character Strings in the original order.
Chars["abc"];  (* => {"a", "b", "c"} *)

Chars["你好👋,世界🌏!"];
(* => {"你", "好", "👋", ",", "世", "界", "🌏", "!"} *)

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

In the example above, "\n" represents the single newline character. In KSL source code, this character can be created with a literal newline or using its Unicode code point #10. The string literal "\n" would be interpreted as a two-character string: "\" and "n".

Chn

Chn[value]

Bidirectional converter between a numeric Unicode code point and its corresponding character.

  • If value is a Number, returns a single-character String for that code point.
  • If value is a String (must be exactly one character), returns its decimal Unicode code point.
Chn["你"];      (* => 20320 *)
Chn["a"];       (* => 97 *)

Chn[97];        (* => "a" *)
Chn[19198];     (* => "䫾" *)

Chn["❤️"];
(*? Error[ksl::builtin::Chn]: Invalid single character: `❤️`. *)

Chn[1919812];
(*? Error[ksl::builtin::Chn]: Invalid unicode: `1919812`. *)

Concat

Concat[item1, item2, ...]

Concatenates a sequence of strings into a single string, or a sequence of lists into a single list. All arguments must be of the same type (either all String or all List).

  • For strings, returns a new concatenated String.
  • For lists, returns a new List containing the elements of all input lists in order (nested lists are not flattened).
Print[Concat["hello,", #10, "world!"]];
(* =>
hello,
world!
*)

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

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

Cond

Cond[branches, fallback]

Evaluates a list of condition-result pairs and returns the result corresponding to the first condition that evaluates to #t. If no condition matches, the fallback is returned.

  • branches must be a list of { condition, result } pairs.
  • Evaluation short-circuits; only the first matching branch is executed.
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

Consume[thread]

Waits for a thread to complete and retrieves its return value. Blocks the current execution until the target thread finishes. A thread can be consumed only once.

  • On first successful call, returns the value produced by the thread.
  • If the thread has already been consumed, returns a status list { #err, "Removed thread." }.
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."}
*)

In the example above, Finished 2 appears first because t2 sleeps for a shorter duration. The main thread then blocks on Consume[t1] until t1 completes and prints Finished 1. The return values of the threads are printed as 1 and 2. The second attempt to consume t1 yields an error status.

Cos

Cos[n]

Computes the cosine of an angle n given in radians.

  • Returns a Number in the range $\left[-1, 1\right]$.
Cos[0];               (* => 1 *)
Cos[3.1415926535898]; (* => -1 *)
Cos[ArcCos[0]];       (* => 6.12323399573677e-17 *)
Cos[-1];              (* => 0.54030230586814 *)

Cosh

Cosh[n]

Computes the hyperbolic cosine of n.

  • Returns a Number greater than or equal to 1.
Cosh[0];    (* => 1 *)
Cosh[1];    (* => 1.54308063481524 *)
Cosh[-1];   (* => 1.54308063481524 *)
Eq[Cosh[1], Cosh[-1]];  (* => #t *)