I wrote the following function in haskell, as it will enumerate every integer:
integers = (0:)$ concat $ zipWith (\x y -> [x,y]) [1..] (map negate [1..])
I wonder if there are better ways to do it, it does seem a bit too complicated.
Also, I wonder are there standard implementations to list all elements in the integer lattice of dimension $k$.
integers = 0 : concat [[x,(-x)] | x <- [1..]]
(or, alternatively, as in @DanielWagner's solution in the comment below that works better I think)
import Control.Applicative
integers = 0 : zipWith (*) ([1..] <* "mu") (cycle [1,-1])
Of course you can take the path of the non-monk as well:
integers = 0 : [y | x <- [1..], y <- [x,-x]]
But then you won't understand the true meaning of mu.
map fun [0 .. ]
where
fun n
| even n = n `quot` 2
| otherwise = (1 - n) `quot` 2
There aren't any standard implementations to list all points in ℤk. Not even for k == 1
, really. But with any enumeration of ℤ and a cartesian product of two lists that outputs any pair at a finite index even if the lists are infinite (some possible implementations here), you can roll your own.
integers :: [Integer]
integers = -- whatever is your favourite implementation
-- get all pairs at finite index, even for infinite input lists
--
cartesian :: [a] -> [b] -> [(a,b)]
cartesian xs ys = ???
-- enumDim k enumerates the points in ℤ^k, to avoid type problems, the points
-- are represented as lists of coordinates, not tuples
enumDim :: Int -> [[Integer]]
enumDim k
| k < 0 = error "Can't handle negative dimensions"
| k == 0 = [[]]
| otherwise = map (uncurry (:)) $ cartesian integers (enumDim (k-1))
-- alternative:
{-
| k == 1 = map return integers
| otherwise = map (uncurry (++)) $ cartesian (enumDim h) (enumDim (k-h))
where
h = k `quot` 2
-}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With