键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Delete

Deletes a property from an object or a file from the filesystem. The function’s behavior is determined by the type and number of arguments provided.

Parameters

Object Deletion

Delete[object, key]

When called with two arguments, Delete operates on an object.

  • object: The Object from which to remove a property.
  • key: An Atom representing the property key to be deleted.

File Deletion

Delete[filepath]

When called with a single string argument, Delete operates on the filesystem.

  • filepath: A String representing the path to the file to be deleted.

    The path can be relative or absolute.

Return Value

Object Deletion

Returns a new Object with the specified key removed.

  • If the key exists, the new object will not contain it.
  • If the key does not exist, the function returns a new object identical to the original.
  • The original object is never modified.

No domain-specific errors are thrown.

File Deletion

Returns Unit[] upon successful deletion.

Errors

An error will occur if the file at filepath does not exist.

Examples

Object Deletion

Let[a, Object[A, { { #a, 10 } }]];
Print[a]; (* => Object[A]{a} *)

(* Returns a new object without the key #a *)
Delete[a, #a]; (* Object[A]{} *)

(* The original object remains unchanged *)
Print[a]; (* => Object[A]{a} *)

(* Rebinding is required to persist the change *)
Let[a, Delete[a, #a]];
(* Deleting a non-existent key has no effect *)
Delete[a, #a]; (* Object[A]{} *)

File Deletion

(* Assuming the file exists, this will delete it and return () *)
Delete["~/Downloads/testFile"]; (* () *)

(* Attempting to delete the same file again will cause an error *)
Delete["~/Downloads/testFile"];
(* ?
Error[ksl::builtin::Delete]: File `~/Downloads/testFile` do not exists.
*)

Div

Performs floating-point division of two numbers.

Parameters

Div[x, y]

The function requires exactly two arguments.

  • x: A Number as the dividend.
  • y: A Number as the divisor.

Return Value

Returns a Number representing the result of $\dfrac{x}{y}$.

In accordance with f64 floating-point standards, the result may be a standard number or inf if the result overflows.

Errors

An error will occur if the divisor y is zero.

Examples

Div[10, 4]; (* 2.5 *)

(* The result can be a very large number *)
Div[1, 1e-308]; (* 1e+308 *)

(* Division by a very small number can result in infinity *)
Div[1, 1e-312]; (* inf *)

(* Division by zero is an error *)
Div[1, 1e-324];
(* ?
Error[ksl::builtin::Div]: Division by zero.
*)

Do

Executes an expression for each element in a list, primarily for side effects.

Parameters

Do[expression, symbol, list]

The function requires exactly three arguments.

  • expression: The expression to be executed for each iteration of the loop.
  • symbol: A Symbol that will be locally bound to the current element of the list during each iteration.
  • list: The List to iterate over.

Return Value

Always returns Unit[], regardless of the content of the loop or the value of the final expression.

No domain-specific errors are thrown by this function.

Examples

(* Prints each number in the list on a new line *)
Do[Print[n], n, { 1, 2, 3, 5, 8, 13 }]; (* () *)

Let[sum, 0];
(* The Let expression modifies the external `sum` variable *)
Do[Let[sum, Add[sum, n]], n, { 1, 2, 3, 5, 8, 13 }]; (* () *)
Print[sum]; (* => 32 *)
Has[n]; (* #f *)

Drop

Returns a new list with the first n elements removed.

Parameters

Drop[list, count]

The function requires exactly two arguments.

  • list: The List from which to drop elements.
  • count: A non-negative Number specifying how many elements to remove from the beginning of the list.

Return Value

Returns a new List.

  • The new list contains the elements of the original list that remain after the first count elements have been removed.
  • If count is greater than or equal to the length of the list, an empty list {} is returned.
  • The original list is not modified.

Errors

An error will occur if the count argument is a negative number.

Examples

(* Drops the first two elements from the list *)
Drop[{ 1, 2, 3 }, 2]; (* {3} *)

(* Dropping all elements results in an empty list *)
Drop[{ 1, 2, 3 }, 3]; (* {} *)

(* Dropping more elements than the list contains also results in an empty list *)
Drop[{ 1, 2, 3 }, 4]; (* {} *)

(* This will cause an error because the count is negative *)
Drop[{ 1, 2, 3 }, -1];
(* ?
Error[ksl::builtin::Drop]: Unexpected value: `-1`.
*)