Card_Trick_Problem_Set_Solutions - ; Discrete Math PS-Card...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
;;; Discrete Math PS-Card ;;; -------------------------------------------- ;;; factorial, combination, permutation procs (define (fact n) (define (helper product count) (if (> count n) product (helper (* count product) (+ count 1)))) (helper 1 1)) (define (comb n r) (define (helper n-product n d-product count) (cond ((> count r) (/ n-product d-product)) (else (helper (* n-product n) (- n 1) (* d-product count) (+ count 1))))) (helper 1 n 1 1)) (define (perm n r) (define (helper product n count) (cond ((> count r) product) (else (helper (* product n) (- n 1) (+ count 1))))) (helper 1 n 1)) ;;; -------------------------------------------- ;;; getting permutations given a s-element list ;;; procs needed are filter, accumulate, remove ;;; flatmap and permutations (define (filter predicate sequence) (cond ((null? sequence) nil) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) (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))) (define (remove item sequence) (filter (lambda (x) (not (= x item))) sequence))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(define (permutations s) (if (null? s) (list nil)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/01/2011.

Page1 / 5

Card_Trick_Problem_Set_Solutions - ; Discrete Math PS-Card...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online