Created
September 15, 2018 17:23
-
-
Save tylerlrhodes/ef77f8376687b9d807830a56f95c900d 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
#lang sicp | |
(define (identity x) x) | |
(define (enumerate-interval low high) | |
(if (> low high) | |
nil | |
(cons low (enumerate-interval (+ low 1) high)))) | |
(define (append list1 list2) | |
(if (null? list1) | |
list2 | |
(cons (car list1) (append (cdr list1) list2)))) | |
(define (accumulate op initial sequence) | |
(if (null? sequence) | |
initial | |
(op (car sequence) | |
(accumulate op initial (cdr sequence))))) | |
(define (flatmap proc seq) | |
(accumulate append nil (map proc seq))) | |
(flatmap identity (list (list 1 2 3) (list 4 5 6))) | |
;' 1 2 3 4 5 6 | |
; (flatmap identity (list 1 2 3)) | |
; results in an error - flatmap expects a sequence of sequences | |
(flatmap (lambda (i) (list i)) '(1 2 3)) | |
; ' 1 2 3 | |
(flatmap | |
(lambda (i) | |
(map (lambda (j) (list i j)) | |
(enumerate-interval 1 (- i 1)))) | |
(enumerate-interval 1 4)) | |
; {{2 1} {3 1} {3 2} {4 1} {4 2} {4 3}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment