creatur-3.0.0: Framework for artificial life experiments.

Portability portable experimental amy@nualeargais.ie None

ALife.Creatur.Util

Contents

Description

Utility functions that don't fit anywhere else.

Synopsis

# Integers

ilogBase :: (Integral a, Integral b, Integral c) => a -> b -> cSource

ilogBase n m returns the greatest integer not greater than the log base n of m.

isPowerOf :: Integral a => a -> a -> BoolSource

n isPowerOf m returns True if n is a power of m (i.e., if there exists an _integer_ k such that m^k = n)

isqrt :: (Integral a, Integral b) => a -> bSource

isqrt n returns the greatest integer not greater than the square root of n.

perfectSquare :: Integral a => a -> BoolSource

perfectSquare n returns True if n is a perfect square (i.e., if there exists an _integer_ m such that m*m = n)

# Arrays

cropRect :: (Int, Int) -> (Int, Int) -> [a] -> Int -> [a]Source

Assuming xs is a sequence containing the elements of a matrix with k columns, cropRect (a,b) (c, d) k xs returns the elements of the submatrix from (a,b) in the upper left corner to (c,d) in the lower right corner). Note: Matrix indices begin at (0,0).

Example: Suppose we have a 4x6 matrix and we want to extract the submatrix from (1,2) to (2,4), as illustrated below.

a b c d e f
g h i j k l    --->   i j k
m n o p q r           o p q
s t u v w x

We can represent the elements of the original matrix as ['a'..'x']. The elements of the submatrix are ['i', 'j', 'k', 'o', 'p', 'q'], or equivalently, "ijkopq". And that is what cropRect (1,2) (2,4) 6 ['a'..'x'] returns.

cropSquare :: Int -> [a] -> [a]Source

Assuming xs is a sequence containing the elements of a square matrix, cropSquare n xs returns the elements of the submatrix of size nxn, centred within the original matrix xs.

Example: Suppose we have a 5x5 matrix and we want to extract the central 3x3 submatrix, as illustrated below.

a b c d e
f g h i j            g h i
k l m n o    --->    l m n
p q r s t            q r s
u v w x y

We can represent the elements of the original matrix as ['a'..'y']. The elements of the submatrix are ['g', 'h', 'i', 'l', 'm', 'n', 'q', 'r', 's'], or equivalently, "ghilmnqrs". And that is what cropSquare 3 ['a'..'y'] returns.

# Sequences

replaceElement :: [a] -> Int -> a -> [a]Source

replaceElement xs n x returns a copy of xs in which the nth element has been replaced with x. Causes an exception if xs has fewer than n+1 elements. Compare with safeReplaceElement.

reverseLookup :: Eq b => b -> [(a, b)] -> Maybe aSource

rotate :: [a] -> [a]Source

safeReplaceElement :: [a] -> Int -> a -> [a]Source

safeReplaceElement xs n x returns a copy of xs in which the nth element (if it exists) has been replaced with x.

shuffle :: RandomGen g => [a] -> Rand g [a]Source

# Bits/Booleans

boolsToBits :: [Bool] -> StringSource

Convert a list of bits to a string of 0s and 1s.

showBin :: (Integral a, Show a) => a -> ShowSSource

Show non-negative Integral numbers in binary.