Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active June 30, 2024 14:41
Show Gist options
  • Save trikitrok/c3152c807c9153312d75 to your computer and use it in GitHub Desktop.
Save trikitrok/c3152c807c9153312d75 to your computer and use it in GitHub Desktop.
#lang racket
(define (grains)
(letrec
([f
(lambda (n)
(cons n
(lambda () (f (* 2 n)) )))])
(f 1)))
(define (stream-for-n-steps stream n)
(letrec ([f
(lambda(i stream-rest)
(if (= i 0)
empty
(cons (car (stream-rest))
(f (- i 1) (cdr (stream-rest))))))])
(f n stream)))
(define (square sqr-num)
(last (stream-for-n-steps grains sqr-num)))
(define total-grains
(lambda ()
(foldr + 0 (stream-for-n-steps grains 64))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment