We are in early beta, and may not have tested Walnut in your browser/OS combination yet, so it may not look perfect. Thanks for your patience! ×
We are in early beta, and the documentation may be incomplete and/or have missing links. Thanks for your patience! ×

Standard modules


The std://grids module is for supporting problems where elements are placed on a rectangular bounded grid.

If your problem uses this module, you will want to add to the problem a config setting called grid_size which is a list with 2 numbers containing the grid with and height. Most functions in this module use that information in their computations. You can also pass an explicit grid_size to the function (if the size is dynamic, or if there is more than 1 grid)

This module does not export procedures


The most important type defined in this module is

type Point(x: Number, y: Number)

which represents bidimensional coordinates, with the x axis going from west to east, and the y axis going from north to south. So Point(0,0) is typically the northwest corner of the grid.

In addition to this, the module documentations sometimes refers to Spatial which is not a type (the name is not actually in the module code), but a set of types following one of these conventions:

  • A Point is considered a Spatial
  • A list of 2 numbers is considered a Spatial
  • A custom value with a position field that is a Spatial is itself a Spatial.
  • A dictionary with a position key that is attached to a Spatial value is itself a Spatial.

In the definition belows, any time that a Spatial type is specified, actually we are referring to any type satisfying the conditions above. The “position” of a spatial object s is

  • s if s is a Point
  • Point(s[0], s[1]) if s is a list.
  • The position of s.position otherwise.

List of Functions

The grids module provides the following functions

  • cells: count the number of cells in the grid
  • find: checks if there is an element with a given position
  • has_row: checks if there is a row of elements
  • in_bounds: checks if a position is inside the grid.

Details for each function can be found below


Type: cells(): Number Type: cells(grid_size: [Number]): Number

Returns the number of cells of the grid (width × height). It uses the grid size specified in the configuration unless explicitly specified. If provided, the grid size must have 2 elements.


Type: find(items: [Spatial], position: Spatial): Boolean

Returns True if any of the given items is at the given position, False otherwise.


Type: has_row(items: [Spatial], count: Number): Boolean

Returns True if there are count items with their positions aligned in a row inside items. Aligned in a row means that they form a horizontal, vertical, or diagonal line, with no interposed positions.

The count argument must be a non-negative integer.


Type: in_bounds(point: Spatial): Boolean Type: in_bounds(point: Spatial, grid_size: [Number]): Boolean

Returns True if the given point is inside the bounds of the grid. It uses the grid size specified in the configuration unless another size is explicitly specified.


This module provides a random number generator and tools to add random noise to values. It does not provide types.

The random seed is intialized from system noise, unless explicitly set by setting a value to the random_seed configuration variable.

List of Functions

  • bernoulli: generates a random boolean variable with a certain probability
  • choice: returns a uniformly chosen value from a list
  • exp: generates a sample from an exponential distribution
  • normal: generates a sample from a gaussian distribution
  • uniform: generates a sample from a uniform distribution in the interval [0..1)
  • uniformint: generates a sample from a discrete uniform distribution in a given interval













List of procedures

  • shuffle: rearranges a list to a random permutation.