Skip to content

Instantly share code, notes, and snippets.

@tylerlrhodes
Created September 15, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlrhodes/ef77f8376687b9d807830a56f95c900d to your computer and use it in GitHub Desktop.
Save tylerlrhodes/ef77f8376687b9d807830a56f95c900d to your computer and use it in GitHub Desktop.
#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