Created
November 19, 2021 20:52
-
-
Save a11ce/8a0e468cc69dc175d75317ac20b07714 to your computer and use it in GitHub Desktop.
Racket translation of "Reversing the technical interview"
This file contains hidden or 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 | |
; read this, and check back here as you go for racket code | |
; https://aphyr.com/posts/340-reversing-the-technical-interview | |
(define (cons h t) | |
(lambda (p) | |
(if p | |
h | |
t))) | |
;;; some extra stuff | |
(define-syntax list | |
(syntax-rules () | |
[(list elem) (cons elem #f)] | |
[(list elem rest ...) (cons elem (list rest ...))])) | |
(define (first l) | |
(l #t)) | |
(define (rest l) | |
(l #f)) | |
;;; ---- | |
(define (nth l n) | |
(when l | |
(if (= 0 n) | |
(l true) | |
(nth (l false) (sub1 n))))) | |
(define (prn-list l) | |
(display "(") | |
(define (loop l) | |
(if (not l) | |
(displayln ")") | |
(begin | |
(display (l true)) | |
(when (l false) | |
(display " ")) | |
(loop (l false))))) | |
(loop l)) | |
(define (reverse l) | |
(define (loop r l) | |
(if l | |
(loop (cons (l true) r) (l false)) | |
r)) | |
(loop #f l)) | |
(prn-list (reverse (cons 1 (cons 2 (cons 3 #f))))) | |
; (prn-list (reverse (list 1 2 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment