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 | |
;; The Church-encoded number N is represented as: | |
;; - A function (λ (f) ...) which takes a function f | |
;; - Returns a function which accepts an argument x | |
;; and applies f N times to x | |
;; two, Church-encoded as a *Racket* lambda | |
(λ (f) (λ (x) (f (f x)))) |
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 | |
;; CIS352 -- 3/19/2024 | |
;; Exam Solutions | |
;; Version A | |
;; (1a) | |
(define (count-n lst n) | |
(match lst |
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 | |
;; PRACTICE Midterm 1 solutions | |
;; Part (a) | |
(define (mul-list l k) | |
(if (empty? l) | |
'() | |
(cons (* (first l) k) (mul-list (rest l) k)))) |
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 | |
;; Some example λ calculus terms in Racket | |
;; ID (identity) applied to ID | |
;; ((λ (x) x) (λ (x) x)) | |
;; can't run this, y is free--but I can still β reduce it | |
;;((λ (x) y) (λ (z) y)) | |
;; ⟶β y <-- error, in Racket, because we don't know what y is |
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
;; CIS352 -- Feb 27, 2023 | |
;; Definitional interpreters for Scheme (warmup to e3) | |
#lang racket | |
(define (expr? e) | |
(match e | |
[(? number? n) #t] | |
[`(+ ,(? expr? e0) ,(? expr? e1)) #t] | |
[(? symbol? x) #t] | |
;; (let ([x (+ y z)]) x) |
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 (expr? e) | |
(match e | |
[(? symbol? x) #t] | |
[`(lambda (,(? symbol? x)) ,(? expr? e-body)) #t] | |
[`(,(? expr? e0) ,(? expr? e1)) #t] | |
[_ #f])) | |
(define (free-vars e) |
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
;; Exercise 2 | |
;; CIS 352 -- Spring 2024 | |
#lang racket | |
(provide (all-defined-out)) | |
;; | |
;; Maps | |
;; |
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
;; Exercise 2 | |
;; CIS 352 -- Spring 2024 | |
#lang racket | |
(provide (all-defined-out)) | |
;; | |
;; Maps | |
;; |
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) ...) |
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)) |
NewerOlder