Created
May 8, 2013 04:57
-
-
Save bradclawsie/5538289 to your computer and use it in GitHub Desktop.
a solution to http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html in chicken scheme.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; blog.jazzychad.net/2012/08/01/array-iteration-problem.html | |
(use srfi-1) | |
(: f ((list-of number) number number -> (list-of number))) | |
(define f | |
(lambda (arr n i) | |
(cond [(< n 0) | |
;; if we are going right-to-left, reverse the list and use the abs count | |
(reverse (f (reverse arr) (abs n) i))] | |
[else | |
;; else walk the list with a counter j, incrementing any i values | |
(letrec ([n_ne_0? (not (eq? n 0))] | |
[g | |
(lambda (arr_ j) | |
(cond | |
[(or (null? arr_) (and n_ne_0? (eq? j n))) | |
;; return rest of list if null, or n != 0 and j == n | |
;; (in which case we want to cease adding, and | |
;; j == n is our termination test | |
arr_] | |
[else | |
;; if the first elt is i, increment it and the counter | |
(let* ([ca (car arr_)] | |
[p (cond [(eq? ca i) 1] [else 0])]) | |
(cons (+ ca p) (g (cdr arr_) (+ j p))))]))]) | |
(g arr 0))]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment