Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Last active January 17, 2026 08:28
Show Gist options
  • Select an option

  • Save kmicinski/4fa69394a59e776fe9c661ca64f51fad to your computer and use it in GitHub Desktop.

Select an option

Save kmicinski/4fa69394a59e776fe9c661ca64f51fad to your computer and use it in GitHub Desktop.
#lang racket
;; CIS352 -- Spring '26
;; Lecture 2
;; Forms
;; All forms are surrounded by parens, they form S-expressions
;; Here's something to remember from today's class
;(f 1 5) ;; A callsite is syntactic point within the code
;; that is a function call.
;; The environment is an association between names and data
;; You can use the define form to add bindings to the environment
;; A binding is a name
;; By default when I open Racket (by which I mean, when a module begins
;; with #lang racket), I have access to all of the forms and functions
;; provided by the "full" Racket language.
(define foo 42)
(define (my-function x)
(+ x 1))
;; A value is something that can be pritively represented by
;; the programming language runtime.
;;
;; A value is the thing that gets returned from a function.
;;
;; There's a difference between an expression and a value.
;;
;; - An expression is programming language syntax
;; - A value is the runtime representation of some data / result
42
"hello"
#t
#f
;;f(x,y) = x * ((y+x) * (x+2))
(define (f x y) (* x (* (+ y x) (+ x 2))))
;(define (+ x y) -20)
;; environments are extended by usage of the form define
(define (b x)
(define (+ x y) (- x y))
(+ x x))
(+ 20 20)
(define (fib x)
(cond [(equal? x 0) 1]
[(equal? x 1) 1]
[else (+ (fib (- x 1)) (fib (- x 2)))]))
;(fib 3)
;(+ (fib 2) (fib 1))
;(+ (+ (fib 1) (fib 0)) (fib 1))
;(+ (+ 1 (fib 0)) (fib 1))
;(+ (+ 1 1) (fib 1))
;(+ 2 (fib 1))
;(+ 2 1)
(define (optimized-fib x)
;; helper function is a function that does some of the
;; hard work of another function, but often adds
;; some extra parameters (or something like that)
;;
;; First we define the helper function, then to accomplish
;; the main goal, we call the helper function with
;; a specific instantiation of arguments
(define (h n a0 a1)
(cond [(equal? n 0) a0]
[else (h (- n 1) a1 (+ a0 a1))]))
(h x 1 1))
;;
;(define (f x) #t)
(define (g x y) y)
;; my challenge: write this code without using cond
(define (baz x y z)
(cond [(f x) y]
[(g x y) z]
[else x]))
#;(define (baz x y z)
(if (f x)
y
(if (g x y)
z
x)))
;; https://quickanswer.harp-lab.com/
(define (a3) 15)
(define (a2) a3)
(define (a1) a2)
(((a1))) ;; call a1 on 0 args returns a2
;; which (calling on 0 args) returns a3
;; which (calling on 0 args) returns 15
;; Lists and recursion
'(1 2 3)
;; quote means: stop interpreting code, *start* building data
;;
;; If I give you a list, what am I really giving you?
;;
;; Without exception, lists are one of two things:
;; - The empty list, "null," '()
;; -> You can check whether something is the empty list by using empty?
;; - A cons cell, where the first argument is some element
;; and the second argument is another list
(equal? '(1 2 3) (cons 1 (cons 2 (cons 3 '()))))
;; The fact that lists are represented in this manner allows
;; me to write recursive functions using a fairly direct
;; "template" / "recipe."
(define (h l)
(if (empty? l)
'base-case
;; compute (h (rest l)) and combine it with (first l)
'recursive-case))
(define (list-length l)
(if (empty? l)
0
(+ 1 (list-length (rest l)))))
;; syntax for working with lists:
;; (empty? l) -- returns #t/#f
;; (cons x0 l) -- prepends x0 to the list l
;; '() -- the empty list
;; (first l) -- l's first element
;; (rest l) -- the rest / tail of the list
;; *don't* use recursion
(define (third l)
'todo)
;; if n = 0, return first l
;; otherwise return the n-1th of the rest of l
(define (nth-l l n)
'todo)
;; quickanswer.harp-lab.com
#;(define (list-length l)
(if (empty? l)
0
(+ 1 (list-length (rest l)))))
;; syntax for working with lists:
;; (empty? l) -- returns #t/#f
;; (cons x0 l) -- prepends x0 to the list l
;; '() -- the empty list
;; (first l) -- l's first element
;; (rest l) -- the rest / tail of the list
;; *don't* use recursion
#;(define (third l)
'todo)
;; if n = 0, return first l
;; otherwise return the n-1th of the rest of l
#;(define (nth-l l n)
'todo)
#;(define (third l)
(first (rest(rest l))))
;; '(3) is equal to (cons 3 '())
;; +----+-----+
;; | 3 | .------> '()
;; +----+-----+
;; Exercises
;; generates n zeros in a row
(define (zeros n)
(if (equal? n 0)
'()
(cons 0 (zeros (- n 1)))))
;; repeat i n times
(define (repeat i n)
(if (equal? n 0)
'()
(cons i (repeat i (- n 1)))))
;; produces (0 .... n-1)
(define (range n)
;; use helper function--call it h
;; idea: use recursion with two parameters:
;; n and i. i starts at 0 and counts up
;; when i = n, return, otherwise return
;; (cons i (h n (+ i 1)))
(define (h i) ;; is my index
(if (equal? i n)
'()
(cons i (h (+ i 1)))))
;; to call the helper function
(h 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment