1 Replies - 3046 Views - Last Post: 05 December 2011 - 04:23 AM

#1 EveilAxe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 01-March 10

question on scheme code, using DrRacket

Posted 03 December 2011 - 05:21 PM

I'm having problem with the calc-depth. The code works for the first three check-expect but the fourth one says that the actual value is 4 but it should be 3. The problem is how do I let the function knows that it should skip the (2) and calc only the highest depth?
These are my code,
;;(calc-depth L)
;;The calc-depth function takes a list L and returns its depth.
;;The depth of a list is defined as the maximum level of "nesting" of a list.
;;A list with no sublists has a depth of 1; a list with sublists that don't have sublists has a depth of 2; a list with sublists that have sublists that don't have sublists has a depth of 3; and so on.
(define calc-depth
  (lambda (L)
    (cond
      ((empty? L) 1)
      ((list? (first L)) (+ (calc-depth (first L)) (calc-depth (rest L))))
      (else (calc-depth (rest L))))))

(check-expect (calc-depth '()) 1)
(check-expect (calc-depth '(1 (2) 3)) 2)
(check-expect (calc-depth '(1 (2 (3) 4) 5)) 3)
(check-expect (calc-depth '(1 (2) 3 (4 (5)))) 3) ;;problem.



Is This A Good Question/Topic? 0
  • +

Replies To: question on scheme code, using DrRacket

#2 ShawnStovall  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 26-May 08

Re: question on scheme code, using DrRacket

Posted 05 December 2011 - 04:23 AM

It looks like it's just counting the lists instead of checking the depth. You have it add to the number every time it encounters a list. It should only add to the number when a new layer is found.

Sorry for the lack of code. I'm pretty new to lisping myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1