键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Range

Range[start, step, end]

Generates a list of numbers from start to end (inclusive), incrementing by step.

  • Returns a List of Numbers.
Range[1, 2, 9];   (* => {1, 3, 5, 7, 9} *)
Range[10, -2, 4]; (* => {10, 8, 6, 4} *)

Read

Read[path]

Read[path, count]

Reads the contents of a file.

  • With a single argument, reads the entire file as a UTF‑8 String.
  • With a count argument, reads count bytes from the file and returns a List of Numbers.
Read["hello.txt"];               (* => "world" *)
Read["/dev/urandom", 4];         (* => {1509190865, 2408416647, 3954905998, 328341581} *)

Read["/dev/urandom"];
(*? Error[ksl::builtin::Read]: Unable to read contents of file at `/dev/urandom` with `operation interrupted`. *)

ReCapture

ReCapture[pattern, s, captures]

Captures values from named groups in the first match of the regular expression pattern within string s.

  • captures is a list of Atoms specifying which named groups to extract.
  • Returns a List of the same length as captures. Each position contains the captured substring if the corresponding named group participated in the match; otherwise Unit[].
  • If the regular expression is invalid or no match is found anywhere in s, an error is raised.
ReCapture[
  "^hello, (?<name>(.+))! Today is (?<state>(.+))\.$",
  "hello, Kevin! Today is good.",
  { #name, #state }
];
(* => {"Kevin", "good"} *)

(* Requesting only a subset of groups *)
ReCapture[
  "^hello, (?<name>(.+))! Today is (?<state>(.+))\.$",
  "hello, Kevin! Today is good.",
  { #name }
];
(* => {"Kevin"} *)

(* Requesting a non‑existent group name *)
ReCapture[
  "^hello, (?<name>(.+))! Today is (?<state>(.+))\.$",
  "hello, Kevin! Today is good.",
  { #oops }
];
(* => {()} *)

ReceiveValue

ReceiveValue[rx]

Receives a value from the channel receiver rx. Blocks until a value is available.

  • Returns the value sent via SendValue.
OpenChannel[rx, tx];
SendValue[tx, "hello"];
ReceiveValue[rx];  (* => "hello" *)

Reduce

Reduce[list, fun, initial]

Reduces (folds) list from left to right using the binary function fun, starting with initial.

  • fun must accept two arguments: the accumulator and the current element.
  • Returns the final accumulated value.
Reduce[{ "a", "b", "c" }, Concat, "!"];  (* => "!abc" *)
Reduce[{ 1, 2, 3 }, Add, 0];             (* => 6 *)

Rem

Rem[m, n]

Computes the remainder of m divided by n, truncating toward zero.

  • Returns a Number.
Rem[7, 3];   (* => 1 *)
Rem[-7, 3];  (* => -1 *)
Rem[7, -3];  (* => 1 *)

ReMatch

ReMatch[pattern, s]

Tests whether the regular expression pattern matches any part of string s.

  • Returns #t if a match is found anywhere in s, otherwise #f.
ReMatch["^[1-9]+$", "1231"];  (* => #t *)
ReMatch["^[1-9]+$", "12301"]; (* => #f *)
ReMatch["[1-9]+", "12301"];   (* => #t *)

Remove

Remove[list, index]

Returns a new list with the element at the 1‑based index removed. The original list is unchanged.

  • Returns a List.
  • An error is raised if index is out of bounds.
Remove[{ 1, 2, 3 }, 2];  (* => { 1, 3 } *)

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

Reverse

Reverse[list]

Returns a new list with the elements of list in reverse order. The original list is unchanged.

  • Returns a List.
Reverse[{ "a", "b", "c" }];  (* => { "c", "b", "a" } *)

Rm

Rm[path]

Removes the file or directory at path recursively (equivalent to rm -r). This operation is irreversible.

  • Returns Unit[].
  • An error is raised if path does not exist or permissions are insufficient.
Rm["/tmp/notable"];
(*? Error[ksl::builtin::Rm]: Failed to get the metadata of `/tmp/notable` with: `No such file or directory (os error 2)` *)

Round

Round[n]

Rounds n to the nearest integer, with halfway cases rounding away from zero.

  • Returns a Number.
Round[3.2];   (* => 3 *)
Round[3.8];   (* => 4 *)
Round[-3.5];  (* => -4 *)

RunShell

RunShell[cmd, args]

Executes a system command with the given arguments. Stdout and stderr are inherited from the KSL process and are not captured.

  • cmd is a String naming the command to run.
  • args is a List of arguments.
    • String arguments are passed verbatim.
    • Atom arguments (e.g., #h) are automatically converted to a flag (-h).
  • Returns #ok if the command exits successfully, or #err if it fails.
  • An error is raised if the command cannot be executed (e.g., command not found).
RunShell["ls", { "-la", #h }];  (* => #ok *)

Mkdir["a"];
RunShell["rm", { "a" }];
(* rm: a: is a directory
  => #err *)

RunShell["nocmd", { }];
(*? Error[ksl::builtin::RunShell]: Execution failed: No such file or directory (os error 2). *)