Created
February 1, 2024 16:00
-
-
Save kmicinski/3672ac89e69679b3ef820f2b611f56af 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 | |
(define l '(2 7 1)) | |
(define l0 (cons 2 (cons 7 (cons 1 '())))) | |
(define (sum-list-of-length-3 l) | |
(define x0 (first l)) | |
(define x1 (second l)) | |
(define x2 (third l)) | |
(+ x0 x1 x2)) | |
;; Primitive recursive functions over lists | |
;; A function that is primitive recursive over a list l | |
;; is one which can be written using this template: | |
#;(define (f l) | |
(if (empty? l) | |
base-case | |
(let ([recursive-answer (f (cdr l))]) | |
(combine (car l) recursive-answer)))) | |
(define (length l) | |
(if (empty? l) | |
0 | |
(add1 (length (cdr l))))) | |
;; Using textual reduction to understand how length executes | |
;; (length '(2 3 8)) | |
;; (add1 (length '(3 8))) | |
;; ... | |
;; (add1 (add1 (length (cdr '(3 8))))) | |
;; ... | |
;;(add1 (add1 (length '(8)))) | |
;;(add1 (add1 (add1 (length '())))) | |
;;(add1 (add1 (add1 0))) | |
(define (sum-list l) | |
(if (empty? l) | |
0 | |
(+ (car l) (sum-list (cdr l))))) | |
;; you'll have to use append, and your solution will be O(n^2) likely.. | |
(define (reverse l) | |
(if (empty? l) | |
'() | |
(append (reverse (cdr l)) (list (first l))))) | |
;; (append '() l1) => l1 | |
(define (append l0 l1) | |
(if (empty? l0) | |
l1 | |
(cons (first l0) (append (rest l0) l1)))) | |
;; (every-other '()) => '() | |
;; (every-other '(1)) => '(1) | |
;; (every-other '(1 2)) => '(1) | |
;; (every-other '(1 2 3)) => '(1 3) | |
;; (every-other '(1 2 3 4)) => '(1 3) | |
(define (every-other l) | |
(cond [(empty? l) '()] | |
[(= (length l) 1) (list (first l))] | |
;; length >= 1 | |
[else (cons (first l) (every-other (rest (rest l))))])) | |
;; This function takes the first n elements from the list l | |
;; assume that the length of l is least n | |
(define (take l n) | |
(if (equal? n 0) | |
'() | |
(cons (first l) (take (rest l) (- n 1))))) | |
(define (drop l n) | |
(if (equal? n 0) | |
l | |
(drop (rest l) (- n 1)))) | |
;; This function will take a game board and will return the rows | |
;; (get-rows '(X O E E E E O E X)) ==> '((X O E) (E E E) (O E X)) | |
;; JUST FOR NOW: ASSUME board SIZE IS 3 | |
(define (get-rows board) | |
;; Basic approach: recur over board, check if it is empty? | |
;; If it is empty?, return '(). | |
;; Otherwise, cons the first three elements from the list | |
;; (using take) to the rest of the rows, which you get | |
;; via the recursive invocation to get-rows and drop | |
(if (empty? board) | |
'() | |
(cons (take board 3) (get-rows (drop board 3))))) | |
;; 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 (get-middle-column board) | |
(define (h list-of-rows) | |
(if (empty? list-of-rows) | |
'() | |
(cons (second (first list-of-rows)) (h (rest list-of-rows))))) | |
(h (get-rows-k board))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment