Last active
December 10, 2015 22:08
-
-
Save pwightman/4499629 to your computer and use it in GitHub Desktop.
My first attempt at Racket.
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
; List comprehension version, more Racket-esque, I'm told. | |
(define (mymap lam l) ; lam is a lambda (see bottom) and l a list | |
(for/list ([i l]) ; i is given element for the iteration, l the list | |
(lam i))) ; The expression to be evaluated on each iteration | |
; From scratch (kind of). Recursively creates a list of the results of calling | |
; a lambda on each element | |
(define (mymap lam l) ; Define a function that takes a lambda and a list. | |
(if (empty? l) ; Base case, if the list is empty: | |
empty ; return an empty list | |
(cons (lam (first l)) ; else, build a list where the first element is the | |
(mymap lam (rest l))))) ; result of running lambda on the first element | |
; and the rest is the result of running 'mymap' | |
; on the remaining elements. | |
; Call mymap, passing in a lambda that will be called on each element. | |
(mymap (lambda (e) ; Pass in a lambda as the first argument. | |
(string-append e "!")) ; Body of lambda. | |
(list "one" "two")) ; Second argument of mymap, a list. | |
; Output: | |
; ("one!" "two!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment