Hanoi, DisplayHail, lastPositionOf

posted Nov 13, 2014, 7:19 PM by Samuel Konstantinovich   [ updated Nov 13, 2014, 7:19 PM ]
;this is what I expected most people to have:
(define (hanoiH start temp end n)
  (if ( > n 0)
      (begin
        (hanoih start end temp (- n 1))
        (begin
          (display "Move ")
          (display start)
          (display " to ")
          (display end)
          (newline)    )
        (hanoih temp start end (- n 1)))
      ;false part omitted
      )) 


(define (hanoiH start temp end n)
  (if ( = n 1)
      (begin ;single move
        (display "Move ")
        (display start)
        (display " to ")
        (display end)
        (newline)    )
      (begin ;multiple moves
        (hanoih start end temp (- n 1))
        ;you can use recursion for the single move in the middle too!
        (hanoih start temp end 1)
        (hanoih temp start end (- n 1)))))



(define (hanoi n)
  (hanoih 1 2 3 n))

(hanoi 2)









(define (nextHail n)
  (if (even? n)
      (/ n 2)
      (+ 1 (* 3 n))))

(define (displayhail n)
  (if (= n 1)
      (display 1)
      (begin
        (display n)
        (newline)
        (displayhail (nextHail n)))))
  
(displayhail 5)








(define (lastH last current n L)
  (cond
    ((null? L) last)
    ((equal? (car L) n) (lastH current (+ current 1) n (cdr L)))
    (else          (lastH last (+ current 1) n (cdr L)))
    )
  )
(define (lastPositionOf n L)
  (lastH 0 1 n L))
(lastPositionOf 'a '(a b a))

(lastPositionOf 'a '(a b a))
Comments