Created
February 1, 2024 17:20
-
-
Save kmicinski/49c6d0fbb95587d579639ef9a138c72e to your computer and use it in GitHub Desktop.
This file contains 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
#lang racket | |
;; (list-ref '(1 2 3) 0) => 1 | |
;; (list-ref '(1 2 3) 1) => 2 | |
;; (list-ref '(1 2 3) 2) => 3 | |
;; assume i < (length l) | |
(define (list-ref l i) | |
(if (equal? i 0) | |
(first l) | |
;; should be a call to (list-ref (cdr l) ...) | |
(list-ref (rest l) (- i 1)))) | |
;; Using a helper function to get the rows | |
(define (get-rows-k board) | |
(define (h b) | |
(if (empty? b) | |
'() | |
(cons (take b (sqrt (length board))) (h (drop b (sqrt (length board))))))) | |
(h board)) | |
(define (diagonal board) | |
(pretty-print board) | |
(define size (sqrt (length board))) | |
;; [0..size) = '(0 1 2) | |
(define (get-ith l) | |
(if (empty? l) | |
'() | |
(begin | |
(displayln "get-ith") | |
(displayln size) | |
(displayln (+ (first l) (* (first l) size))) | |
(cons (list-ref board (+ (first l) (* (first l) size))) | |
(get-ith (rest l)))))) | |
(get-ith (range size))) | |
;; these functions are equivalent | |
(define f (lambda (x) x)) | |
(define (f-again x) x) | |
(define (foo x y z) (+ x y z)) | |
;; turned into ... | |
;(define foo (lambda (x y z) (+ x y z))) | |
(define (double g) (lambda (x) (g (g x)))) | |
;; To understand this... | |
;; (double (lambda (x) (+ x 1))) | |
;; We substitute the lambda in for g | |
;;(lambda (x) (add1 (add1 x))) | |
((double (lambda (x) (* x 2))) 4) | |
((double (lambda (x) (+ x 5))) 2) | |
(define (bar f) | |
(define abs (lambda (x) (if (< x 0) (- x) x))) | |
;;(define (abs x) (if (< x 0) (- x) x)) | |
(lambda (x) (f (abs x)))) | |
(define (baz f) (λ (x y) (+ x (f x y)))) | |
((baz (λ (x y) x)) 3 4) | |
;; If the predicate f true for every element of the list l | |
;; (forall (λ (x) (> x 0)) '(0 1 2 3 5)) => #f | |
(define (andmap f l) | |
(cond | |
[(empty? l) #t] | |
[else (and (f (first l)) (andmap f (rest l)))])) | |
;; (exists (λ (x) (> x 0)) '(0 1 2 3 5)) => #t | |
(define (ormap f l) | |
(cond | |
[(empty? l) #f] | |
[else (or (f (first l)) (ormap f (rest l)))])) | |
;; use ormap and andmap to check if any list in lsts (a list of lists of symbols) | |
;; has the property that every element is 'X | |
(define (is-any-list-all-X lsts) | |
;; does *any* list (called lst in each iteration) satisfy the property... | |
(ormap | |
;; the property... | |
;; "are all elements equal? to 'X?" | |
(λ (lst) (andmap (λ (e) (equal? e 'X)) lst)) | |
lsts)) | |
;; this function will make a "counter" | |
(define (mk-counter n) | |
(λ () (cons n (mk-counter (+ n 1))))) | |
((cdr ((cdr ((mk-counter 7)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment