Skip to content

Instantly share code, notes, and snippets.

@zane
Created April 9, 2009 01:07
Show Gist options
  • Save zane/92157 to your computer and use it in GitHub Desktop.
Save zane/92157 to your computer and use it in GitHub Desktop.
;; integer, integer -> (Listof integer)
;;
;; Produces the starting indeces of all the diagonals of a board with the provided dimensions.
(define (list-starter-indeces width height)
(list-starter-indeces-helper 0 0 width height))
;; integer, integer, integer, integer -> (Listof integer)
;;
;; Produces the starting indeces of all the diagonals of a board with the provided dimensions, starting at the provided index.
(define (list-starter-indeces-helper x y width height)
(cond [(and (= x (sub1 width))
(= y (sub1 height))) ; in the lower right corner
(list (list x y))]
[(= y (sub1 height)) ; along the bottom
(cons (list x y)
(list-starter-indeces-helper (add1 x)
y
width
height))]
[(= x 0) ; along the left side
(cons (list x y)
(list-starter-indeces-helper x
(add1 y)
width
height))]))
(check-expect (list-starter-indeces 1 1)
'((0 0)))
(check-expect (list-starter-indeces 3 3)
'((0 0)
(0 1)
(0 2)
(1 2)
(2 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment