Skip to content

Instantly share code, notes, and snippets.

@jestinepaul
Created November 27, 2011 14:37
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 jestinepaul/1397636 to your computer and use it in GitHub Desktop.
Save jestinepaul/1397636 to your computer and use it in GitHub Desktop.
Introduction to Functional Programming - Part 2
(if (zero? 0) 4
(+ "hello" 4.5 '(8 2)))
(define (flatten x)
(cond ((null? x) '())
((list? (car x)) (append (flatten (car x)) (flatten (cdr x))))
(else (cons (car x) (flatten (cdr x))))))
(flatten '(1 (2 "3") "4" ((5))))
(define (sorted? numbers)
(or (< (length numbers) 2)
(and (<= (car numbers) (cadr numbers))
(sorted? (cdr numbers)))))
(sorted? '(1 3 4 5))
(sorted? '(1 4 5 3))
(sorted? '())
(sorted? '(3.4))
(sorted? '(1/3 4/5 7/2 12/5))
;(sorted? '("Alice" "Bobby" "Carol"))
(define (string-sequence-sorted? strings)
(or (< (length strings) 2)
(and (<= (car strings) (cadr strings))
(string-sequence-sorted? (cdr strings)))))
(string-sequence-sorted? '("Alice" "Bobby" "Carol"))
(define (sorted? sequence comp)
(or (< (length sequence) 2)
(and (comp (car sequence) (cadr sequence))
(sorted? (cdr sequence) comp))))
(sorted? '(1 4 5 7 8 10 14) <)
(sorted? '(8.5 4.3 2.0 0.4 -1.3 -5.5) >)
(sorted? '(8.5 4.3 2.0 0.4 -7.3 -5.5) >)
(sorted? '("apple" "banana" "banana" "cherry") string<=?)
(define (list-length<? ls1 ls2) (< (length ls1) (length ls2)))
(sorted? '((1) ("a" 'b) ((#t) 4.5 (10/3 8+7i))) list-length<?)
(define (my-unary-map fn seq)
(if (null? seq) '()
(cons (fn (car seq))
(my-unary-map fn (cdr seq)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment