Created
March 17, 2017 06:56
-
-
Save wobh/e483cd6f52ee6c1ad8b807bfadc019fb to your computer and use it in GitHub Desktop.
Acing the technical interview (in Common Lisp)
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
;; Inspired by https://aphyr.com/posts/340-acing-the-technical-interview | |
(defun conz (head tail) | |
(lambda (_) (if _ head tail))) | |
(let ((subject (conz 1 (conz 2 nil)))) | |
(assert (= 1 (funcall subject t))) | |
(assert (= 2 (funcall (funcall subject nil) t)))) | |
(defun mth (lizt mth) | |
(labels ((mth-r (lizt mth) | |
(when lizt | |
(if (zerop mth) | |
(funcall lizt t) | |
(mth-r (funcall lizt nil) | |
(1- mth)))))) | |
(mth-r lizt mth))) | |
(let ((subject (conz 1 (conz 2 nil)))) | |
(assert (= 1 (mth subject 0))) | |
(assert (= 2 (mth subject 1))) | |
(assert (null (mth subject 2))) | |
(assert (null (mth subject -1)))) | |
(defun pprint-lizt (lizt) | |
(labels ((pprint-lizt-r (lizt) | |
(cond ((null lizt) | |
(princ ")") | |
(terpri)) | |
(t (princ (funcall lizt t)) | |
(when (funcall lizt nil) | |
(princ " ")) | |
(pprint-lizt-r (funcall lizt nil)))))) | |
(princ "(") | |
(pprint-lizt-r lizt))) | |
(let ((subject (conz 1 (conz 2 (conz 3 nil)))) | |
(*standard-output* (make-string-output-stream))) | |
(pprint-lizt subject) | |
(assert (equal (get-output-stream-string *standard-output*) | |
"(1 2 3) | |
"))) | |
(defun reverz (lizt) | |
(labels ((reverz-r (tzil lizt) | |
(if lizt | |
(reverz-r (conz (funcall lizt t) | |
tzil) | |
(funcall lizt nil)) | |
tzil))) | |
(reverz-r nil lizt))) | |
(let ((subject (conz 1 (conz 2 (conz 3 nil)))) | |
(*standard-output* (make-string-output-stream))) | |
(pprint-lizt (reverz subject)) | |
(assert (equal (get-output-stream-string *standard-output*) | |
"(3 2 1) | |
"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment