Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Created August 26, 2019 20:35
Show Gist options
  • Save pedrominicz/67531948cb6d7fcc07ccd708ef75110d to your computer and use it in GitHub Desktop.
Save pedrominicz/67531948cb6d7fcc07ccd708ef75110d to your computer and use it in GitHub Desktop.
Ninety-Nine Racket Problems
#lang plai-typed
; Problem 1
(define (my-last list)
(if (<= (length list) 1)
list
(my-last (rest list))))
;(test (my-last empty) empty)
;(test (my-last (list 1)) (list 1))
;(test (my-last (list 1 2 3)) (list 3))
; Problem 2
(define (my-but-last list)
(if (<= (length list) 2)
list
(my-but-last (rest list))))
;(test (my-but-last empty) empty)
;(test (my-but-last (list 1)) (list 1))
;(test (my-but-last (list 1 2 3)) (list 2 3))
; Problem 3
(define (element-at list n)
(cond
[(= n 1) (first list)]
[else (element-at (rest list) (- n 1))]))
; Problem 3
(define (my-length list)
(if (empty? list)
0
(+ 1 (my-length (rest list)))))
; Problem 4
(define (my-reverse list)
(if (empty? list)
empty
(append (my-reverse (rest list)) (cons (first list) empty))))
; Problem 5 (TODO)
(define (is-palindrome list)
false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment