Last active
August 29, 2015 14:17
-
-
Save mtthwgry/2d7f0a76ca7cc2adb5f4 to your computer and use it in GitHub Desktop.
What a fucking Racket...I'm hooked...
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 | |
; sq : number -> number | |
(define (sq num) (* num num)) | |
; f-to-c : number -> number | |
(define (f-to-c temp) (/ (- temp 32) 1.8)) | |
; how-cold : integer -> string | |
(define (how-cold temp) | |
(if (< (f-to-c temp) 0) "brrr" "could be worse")) | |
; pipe-surface-area : integer integer -> double | |
(define (pipe-surface-area length diameter) | |
(* pi (* length diameter))) | |
; spanish-greeting : string -> string | |
(define (spanish-greeting name) | |
(format "Hola ~a!" name)) | |
; english-greeting : string -> string | |
(define (english-greeting name) | |
(format "Hello ~a!" name)) | |
; spanish? : string -> boolean | |
(define (spanish? language) | |
(equal? language "spanish")) | |
; english? : string -> boolean | |
(define (english? language) | |
(equal? language "english")) | |
; greet : string string -> string | |
(define (greet name language) (cond | |
[(spanish? language) (spanish-greeting name)] | |
[(english? language) (english-greeting name)])) | |
; add1 : integer -> integer | |
(define (add1 int) | |
(+ int 1)) | |
; list-length : list integer -> integer | |
(define (list-length list [count 0]) | |
(if (empty? list) | |
count | |
(list-length (cdr list) (add1 count)))) | |
; search : list object -> integer | |
(define (search list item [index 0]) | |
(if (empty? list) | |
-1 | |
(if (equal? (car list) item) | |
index | |
(search (cdr list) item (add1 index))))) | |
; sub1 : integer -> integer | |
(define (sub1 int) | |
(- int 1)) | |
; zero? : integer -> boolean | |
(define (zero? int) | |
(equal? int 0)) | |
; item-at : list integer -> object | |
(define (item-at list pos) | |
(if (zero? pos) (car list) (item-at (cdr list) (sub1 pos)))) | |
; first-and-last : list -> list | |
(define (first-and-last input) | |
(list (car input) (item-at input (sub1 (list-length input))))) | |
; even? : integer -> boolean | |
(define (even? int) | |
(equal? (remainder int 2) 0)) | |
; just-the-evens : list list -> list | |
(define (just-the-evens input [result (list )]) | |
(if (empty? input) | |
result | |
(if (even? (car input)) | |
(just-the-evens (cdr input) (append result (list (car input)))) | |
(just-the-evens (cdr input) result)))) | |
; select : list function list -> list | |
(define (select input filter [result `()]) | |
(if (empty? input) | |
result | |
(if (filter (car input)) | |
(select (cdr input) filter (append result (list (car input)))) | |
(select (cdr input) filter result)))) | |
; odd? : integer -> boolean | |
(define (odd? int) | |
(not (equal? (remainder int 2) 0))) | |
; triple-all : list list -> list | |
(define (triple-all lst [result `()]) | |
(if (empty? lst) | |
result | |
(triple-all (cdr lst) (append result (list (triple (car lst))))))) | |
; triple : integer -> integer | |
(define (triple int) | |
(* int 3)) | |
; sum : list -> integer | |
(define (sum lst) | |
(apply + lst)) | |
; sum-even-triples : list -> integer | |
(define (sum-even-triples lst) | |
(sum (select (triple-all lst) even?))) | |
; contains-only-lower-case? : list boolean -> boolean | |
(define (contains-only-lower-case? lst [result #t]) | |
(if (empty? lst) | |
result | |
(if result | |
(if (char-lower-case? (car lst)) | |
(contains-only-lower-case? (cdr lst) #t) | |
(contains-only-lower-case? (cdr lst) #f)) | |
result))) | |
; map : list function -> list | |
(define (map lst fn [result `()]) | |
(if (empty? lst) | |
result | |
(map (cdr lst) fn (append result (list (fn (car lst))))))) | |
; greet-spanish : string -> string | |
(define (greet-spanish name) | |
(greet name "spanish")) | |
; greet-english : string -> string | |
(define (greet-english) | |
(lambda (name) | |
(greet name "english"))) | |
(sq 20) | |
(f-to-c 68) | |
(how-cold 68) | |
(pipe-surface-area 12 4) | |
(greet "Matthew" "english") | |
(greet "Cassie" "spanish") | |
(list-length `(1 2 3 4 5 6)) | |
(search `(1 2 3) 1) | |
(search `(1 2 3) 5) | |
(item-at `(1 2 3 4 5) 4) | |
(first-and-last `(1 2 3 4 5 6 7 8 9 0)) | |
(just-the-evens `(1 2 3 4 6 8 10 11 13 15 16)) | |
(select `(1 2 3 4 6 8 10 11 13 15 16) even?) | |
(select `(1 2 3 4 6 8 10 11 13 15 16) odd?) | |
(sum `(1 2 3)) | |
(triple-all `(1 2 3)) | |
(sum-even-triples `(1 2 3 4 5 6)) | |
(contains-only-lower-case? `(#\b #\c #\d #\a)) | |
(contains-only-lower-case? `(#\b #\c #\d #\A)) | |
(map `(1 2 3 4 5) triple) | |
((greet-english) "Cassie") | |
(greet-spanish "Mateo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment