Very simple function this time, but therefore also very easy to write in a classical rather than lispy way. Given a 2 dimensional array such as made by (def makeworld (x)
(= world (n-of x (n-of x nil))))
(def showpos (row col)
(errsafe ((world row) col)))
and filled with some numbers, how would you show the immediate surroundings of a number. A classical way would be to use two for loops and a bucket (def observe (x y r)
(let b nil
(for i (* r -1) r
(for j (* r -1) r
(if (showpos (+ x j) (+ y i))
(= b (cons (showpos (+ x j) (+ y i)) b))
)))
b))
What I've been playing with is using each, range and consif, but it still is the same structure. Any alternatives? maybe getting rid of the bucket by using accum? (def observe (row col ran)
(let bucket nil
(each dr (range (* -1 ran) ran)
(each dc (range (* -1 ran) ran)
(= bucket (consif (showpos (+ row dr) (+ col dc)) bucket))))
bucket))
|