Problem_Set_03_Solutions_Code

# Problem_Set_03_Solutions_Code - Discrete math PS-3;=...

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

;;; Discrete math PS-3 ;;;=========================================================== ;;; Exercise 2 ;;;=========================================================== ;;; 4-peg Tower of Hanoi - toh4-sloppy ;;; if n is odd, the top-half gets an extra disk (define (toh4-sloppy n from to using1 using2) (let ((top-half (ceiling (/ n 2))) (bottom-half (floor (/ n 2)))) (cond ((= n 1) (display (list 'toh4-sloppy: 'move 'disk from '--> to)) (newline)) (else (toh4-sloppy top-half from using1 to using2) (toh4-sloppy bottom-half from to using2 using1) (toh4-sloppy top-half using1 to from using2))))) ;; works for n = 3 ;(toh4-sloppy 3 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> to) ;; works for n = 4 ;(toh4-sloppy 4 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using2 --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> to) ;; does not work for n = 8 ;(toh4-sloppy 8 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk using2 --> using1) ;(toh4-sloppy: move disk to --> from) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk to --> using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to)

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

View Full Document
;(toh4-sloppy: move disk using2 --> from) ;(toh4-sloppy: move disk using2 --> to) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk to --> from)
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

Problem_Set_03_Solutions_Code - Discrete math PS-3;=...

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

View Full Document
Ask a homework question - tutors are online