Card_Trick_Problem_Set_Solutions

# Card_Trick_Problem_Set_Solutions - ; Discrete Math PS-Card...

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

;;; 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))

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

View Full Document
(define (permutations s) (if (null? s) (list nil)
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
Ask a homework question - tutors are online