solutions

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

HW4

posted Sep 26, 2012, 6:52 PM by Samuel Konstantinovich   [ updated Sep 26, 2012, 6:52 PM ]

;1 Define a quotient function without using quotient
(define myQuotient (lambda (a b) (/ (- a (modulo a b))b)))
;OR
(define myQuotient (lambda (a b)(-(/ a b)(/(modulo a b)b))))

;2 Is the Nth place even
;by defining a 2nd function
(define getNthPlace (lambda (x place)(modulo (quotient x place)10)))
(define isTheNthPlaceEven? (lambda (x place) (even? (getNthPlace x place))))
;OR writing it as one function
(define isTheNthPlaceEven? (lambda (x place) (even? (modulo (quotient x place)10))))

;3 Gravity is rad...
;two ways to define G:
(define G (* 6.673 0.00000000001))
(define G 6.673e-011)
;The function itself:
(define gravity (lambda (m1 m2 D)(/(* m1 m2 G)(* D D))))

1-2 of 2