Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created January 28, 2026 20:01
Show Gist options
  • Select an option

  • Save kmicinski/60fa61d9ebe45ecaa7beb295d4fa668a to your computer and use it in GitHub Desktop.

Select an option

Save kmicinski/60fa61d9ebe45ecaa7beb295d4fa668a to your computer and use it in GitHub Desktop.
Office Hours Code
#lang racket
;; Define a function that evaluates to #t if
;; the list l contains the number 7
;;
;; - what do we do when l is '()
;; - what do we do when l is (cons hd tl)
(define (contains-7 l)
(if (empty? l)
#f
;; we know it's false
(if (equal? (first l) 7)
#t
(contains-7 (rest l)))))
;; (sum-list l) returns the list of l for *any* list l
(define (sum-list l)
(if (empty? l)
0 ;; (sum-list '()) => 0
(+ (first l) (sum-list (rest l))))) ;;
#;(define (sum-list l)
(if (empty? l)
0 ;; (sum-list '()) => 0
(let ([a0 (first l)])
(let ([a1 (rest l)])
(let ([a2 (sum-list a1)])
(+ a0 a2))))))
;; a list is either '() or (cons x y)
;; struct Cons {
;; usize car;
;; usize cdr;
;; }
;; takes a list l, adds 1 to every element of l
;; (listof number?) -> (listof number?)
;;
;; (add1-to-all '()) => '()
;; (add1-to-all '(3)) => '(4)
;; (add1-to-all '(1 2 3)) => '(2 3 4)
(define (add1-to-all l)
(if (empty? l)
'()
(cons (+ 1 (first l)) (add1-to-all (rest l)))))
;; (add1-to-all '(0 1 2 3 ... 999)) => '(1 2 ... 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment