i’m kinda of confused how i can construct a for loop in scheme. the for-loop should be implemented in Part II. where it takes a list of numbers and insert each element inside the list in Part I to find the length. I was cable to get the first element but i need a for loop or what so ever to get an output like this: ‘(7 10 5 16 106 37)
here is my code :
#lang racket
; Part I
(define (sequence n)
(cond [(= n 1)
(list n)]
[(even? n)
( cons n(sequence( / n 2)))]
[(odd? n)
( cons n(sequence (+(* n 3) 1))) ] ))
(sequence 3)
; Part II
(define (find-length items)
( cond [(null? items)
(list items)]
[find-length(length(sequence(car items))) ]
))
(find-length '(10 13 16 22 95 158))
here is the output :
'(3 10 5 16 8 4 2 1)
7
Let me get this straight, you need the length of the Collatz sequence for each of the numbers in the
itemslist? Clearly this is homework, so I can’t give a straight answer this time. Here’s the general structure of the solution, fill-in the blanks:Test the procedure, the result should be as shown below:
Notice that your answer was almost right – the base case for this procedure is simply the empty list, and you forgot to call the recursion. In Scheme, at least for know, try not to think about while, for loops: implement iteration in terms of recursion, that’s the idiomatic way to do it. After you get that straight, you can start using one of the built-in looping constructs available in Racket.