<commit_before>
#lang racket

(provide (all-defined-out)) ;; so we can put tests in a second file


; 1
(define (sequence low high stride)
  (if (<= low high)
    (cons low (sequence (+ low stride) high stride))
    null))


; 2
(define (string-append-map xs suffix)
  (map (lambda (x) (string-append x suffix)) xs))


; 3
(define (list-nth-mod xs n)
  (cond [(< n 0) (error "list-nth-mod: negative number")]
        [(null? xs) (error "list-nth-mod: empty list")]
        [else (list-ref xs (remainder n (length xs)))]))
        
; 4
(define (stream-for-n-steps s n)
  (if (<= n 0)
      null
      (cons (car (s)) (stream-for-n-steps (cdr (s)) (- n 1)))))

; 5
(define (funny-number-stream)
  (define (stream x)
    (cons (if (= (remainder x 5) 0) (- x) x)
          (lambda () (stream (+ x 1)))))
  (stream 1))

; 6
(define (dan-than-dog)
  (define (stream x)
    (cons x
          (lambda ()
            (stream (if (equal? x "dan.jpg") "dog.jpg" "dan.jpg")))))
  (stream "dan.jpg"))

; 7
(define (stream-add-zero s)
  (lambda ()
    (cons (cons 0 (car (s)))
          (stream-add-zero (cdr (s))))))

; 8
(define (cycle-lists xs ys)
  (define (stream n)
    (lambda ()
      (cons (cons (list-ref xs (remainder n (length xs)))
                  (list-ref ys (remainder n (length ys))))
            (stream (+ n 1)))))
  (stream 0))

; 9
(define (vector-assoc v vec)
  (define (iter n)
    (if (>= n (vector-length vec))
        #f
        (let ((cur (vector-ref vec n)))
          (cond [(not (pair? cur)) (iter (+ n 1))]
                [(equal? (car cur) v) cur]
                [#t (iter (+ n 1))]))))
  (iter 0))
  
<commit_msg>10: cached-assoc and 1 test (will add more tests soon)<commit_after>
#lang racket

(provide (all-defined-out)) ;; so we can put tests in a second file


; 1
(define (sequence low high stride)
  (if (<= low high)
    (cons low (sequence (+ low stride) high stride))
    null))


; 2
(define (string-append-map xs suffix)
  (map (lambda (x) (string-append x suffix)) xs))


; 3
(define (list-nth-mod xs n)
  (cond [(< n 0) (error "list-nth-mod: negative number")]
        [(null? xs) (error "list-nth-mod: empty list")]
        [else (list-ref xs (remainder n (length xs)))]))
        
; 4
(define (stream-for-n-steps s n)
  (if (<= n 0)
      null
      (cons (car (s)) (stream-for-n-steps (cdr (s)) (- n 1)))))

; 5
(define (funny-number-stream)
  (define (stream x)
    (cons (if (= (remainder x 5) 0) (- x) x)
          (lambda () (stream (+ x 1)))))
  (stream 1))

; 6
(define (dan-than-dog)
  (define (stream x)
    (cons x
          (lambda ()
            (stream (if (equal? x "dan.jpg") "dog.jpg" "dan.jpg")))))
  (stream "dan.jpg"))

; 7
(define (stream-add-zero s)
  (lambda ()
    (cons (cons 0 (car (s)))
          (stream-add-zero (cdr (s))))))

; 8
(define (cycle-lists xs ys)
  (define (stream n)
    (lambda ()
      (cons (cons (list-ref xs (remainder n (length xs)))
                  (list-ref ys (remainder n (length ys))))
            (stream (+ n 1)))))
  (stream 0))

; 9
(define (vector-assoc v vec)
  (define (iter n)
    (if (>= n (vector-length vec))
        #f
        (let ((cur (vector-ref vec n)))
          (cond [(not (pair? cur)) (iter (+ n 1))]
                [(equal? (car cur) v) cur]
                [#t (iter (+ n 1))]))))
  (iter 0))

; 10
(define (cached-assoc xs n)
  (let ([cache (make-vector n #f)]
        [counter 0])
    (define (helper-assoc v)
      (let ([cached-answer (vector-assoc v cache)])
        (if (pair? cached-answer)
            cached-answer
            (let ([found-answer (assoc v xs)])
              (if (pair? found-answer)
                  (begin (vector-set! cache (remainder counter n) found-answer)
                         (set! counter (+ 1 counter))
                         found-answer)
                  #f)))))
    helper-assoc))


{'fraction_non_alphanumeric': 0.18538741549661986, 'fraction_numerical': 0.01326053042121685, 'mean_word_length': 2.1049233252623085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5402858', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1383, 'n_words': 455}