Created
March 8, 2018 13:28
-
-
Save jamesmintram/cdf9834e6ba4cd391de076160a55fe2a to your computer and use it in GitHub Desktop.
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
;------------------------------------------------ | |
; Playing around with Racket | |
;------------------------------------------------ | |
(define app | |
(lambda (lst1 lst2) | |
(if (null? lst1) | |
lst2 | |
(cons (car lst1) (app (cdr lst1) lst2 ))))) | |
(app '(1 2 3) '(3 4 5)) | |
;------------------------------------------------ | |
(define map | |
(lambda (m f) | |
(if (null? m) | |
'() | |
(cons (f (car m)) (map (cdr m) f))))) | |
(map '(1 2 3) (lambda (v) (* 2 v))) | |
;------------------------------------------------ | |
(define foldl | |
(lambda (seq fn val) | |
(if (null? seq) | |
val | |
(foldl (cdr seq) fn (fn val (car seq)))))) | |
(foldl '(1 2 3 4 5 6) (lambda (a b) (* a b)) 1) | |
;------------------------------------------------ | |
(define foldr | |
(lambda (seq fn val) | |
(if (null? seq) | |
val | |
(fn val (foldr (cdr seq) fn (car seq)))))) | |
(foldr '(1 2 3 4 5 6) (lambda (a b) (* a b)) 1) | |
;------------------------------------------------ | |
(define partial | |
(lambda (f p1) | |
(lambda (p2) | |
(f p1 p2)))) | |
(define mapf | |
(lambda (fn . params) | |
(map params fn) | |
)) | |
(mapf (partial + 1) 1 2 3) | |
;------------------------------------------------ | |
(define take | |
(lambda (seq n) | |
(if (or (= 0 n) (null? seq)) | |
'() | |
(cons (car seq) (take (cdr seq) (- n 1)))))) | |
(take '(1 2 3 4) 2) | |
;------------------------------------------------ | |
(define rest | |
(lambda (seq n) | |
(if (or (= 0 n) (null? seq)) | |
seq | |
(rest (cdr seq) (- n 1))))) | |
(rest '(1 2 3 4) 2) | |
;------------------------------------------------ | |
(define split | |
(lambda (head tail n) | |
(if (or (= 0 n) (null? tail)) | |
(list head tail) | |
(split (cons (car tail) head) (cdr tail) (- n 1))))) | |
(split '() '(1 2 3 4 5) 2) | |
;------------------------------------------------ | |
(define partition | |
(lambda (seq n) | |
(if (< (length seq) n) | |
(list seq) | |
(cons (take seq n) (partition (rest seq n) n))))) | |
(partition '(1 2 3 4 5 6 7 8) 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment