键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Syntax

The syntax of KSL is similar to Wolfram, but KSL does not support infix notation in its design, making it more similar to LISP.

The syntax of KSL can be broadly categorized into the following classes:

  • Basic elements
  • Lists
  • Function calls

Basic Elements

The most basic elements in KSL are of four types: symbols, atoms, strings, and numbers.

Symbols

Symbols are names, such as a or Add. Symbols must start with a letter, and can be followed by letters, digits, single quotes, and underscores. For example, a____'''''___ is a valid symbol, although it is unlikely to be used in practice.

Atoms

Atoms are similar to enumerations or label types in other languages. The basic syntax is a hash symbol # followed by a name, which follows the same naming rules as symbols. For example, #some'tags. By default, boolean values, true and false, are also atoms, represented as #t and #f. Atoms are often used to represent result states, such as #ok and #err.

Strings

Strings are sequences of characters enclosed in double quotes, including newline characters and special symbols. In KSL, strings do not support escape characters. To insert special characters, you can use the format # + corresponding Unicode code point.

For example:

Let[s, "Hello, world🌏,
Line breaks are also supported, but they will not be automatically aligned,
    which means that if there are leading whitespace characters,
the string will also include these whitespace characters."];
Print[s];
(* =>
Hello, world🌏,
Line breaks are also supported, but they will not be automatically aligned,
    which means that if there are leading whitespace characters,
the string will also include these whitespace characters.
*)

(* Insert a newline `#10` *)
Let[s', Concat[s, #10, "another text here~"]];
Print[s'];
(* =>
Line breaks are also supported, but they will not be automatically aligned,
    which means that if there are leading whitespace characters,
the string will also include these whitespace characters.
another text here~
*)

Numbers

In KSL, numbers default to f64. Numbers can omit the fractional part, but the integer part cannot be omitted, i.e., 12 is a valid numeric literal, but .12 is not. Additionally, numbers can be input in scientific notation format, i.e., 12.34e56 is a valid numeric literal.

Lists

Lists are represented using curly brackets, and the elements within the brackets can be of different types, i.e., {1, "2", #a3} is a valid list literal. Furthermore, lists can be nested, such as {1, 2, {3, {4}}}. In general, functions that may return an error will return the error in the format {#err, "error message"}.

Function calls

In KSL, function calls use square brackets, i.e., Unit[]. All variable names, including function names, are symbols, and all built-in functions start with uppercase letters.

Note that although KSL has anonymous functions, it does not support immediate execution, which means that the following code is incorrect:

Fun[{n}, Add[n, 1]][2]; (* Error! *)

The correct approach is to use the Apply function or to bind the function to a variable before using it, i.e.:

(* method 1 *)
Apply[Fun[{n}, Add[n, 1]], 2]; (* => 3 *)

(* method 2 *)
Let[add1, Fun[{n}, Add[n, 1]]];
add1[3]; (* => 4 *)