new user
new user

Reputation: 75

consing values to a list

I want to make a list of #t/#f statements according to the sequence binary-e. If the value in the binary-e is 0 the value put in lst should be #t, or if it's 1 it should be #f. n argument is how long the lst is supposed to be. However it always returns an empty list. Here's my code:

(define (mysequence n)                  
      (define binary-e (list 0 0 1 1 0 0 1 0 0 0 1 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1))

         (define (makelist lst k)
            (cond((= k (- n 1)) lst)        
                ((= 0 (list-ref binary-e k)) (begin (cons #t lst) (makelist lst (+ k 1)) ))           
                ((= 1 (list-ref binary-e k)) (begin (cons #f lst) (makelist lst (+ k 1))))

            )   
         )


      (makelist '() 0)      
)       

Thanks for any help.

Upvotes: 1

Views: 134

Answers (1)

Óscar López
Óscar López

Reputation: 236004

You can easily solve this one by using map:

(map (lambda (e)
       (if (= e 0) #t #f))
     binary-e)

Or even shorter:

(map zero? binary-e)

But if you need to write a solution from scratch, I'm afraid that the code in the question is far from near a correct answer. I'll give you some pointers and show you the right structure of the solution, so you can find the answer yourself (because this looks a lot like homework), but you'll have to completely re-think your answer. For starters, you don't need to pass along the size of the list:

(define (mysequence lst)
  (cond ((<???> lst)                  ; if the list is empty
         <???>)                       ; then return the empty list
        ((= <???> <???>)              ; if the first element in the list is 0
         (cons <???>                  ; then `cons` #t
               (mysequence <???>)))   ; and process the rest of the list
        ((= <???> <???>)              ; if the first element in the list is 1
         (cons <???>                  ; then `cons` #f
               (mysequence <???>))))) ; and process the rest of the list

Or even shorter:

(define (mysequence lst)
  (if (<???> lst)            ; if the list is empty
      <???>                  ; then return the empty list
      (cons                  ; else `cons`
       <???>                 ; if the first element in list is zero #t else #f
       (mysequence <???>)))) ; process the rest of the list

Either way, call it like this:

(define binary-e <???>) ; define the list outside the procedure
(mysequence binary-e)   ; and pass it along as a parameter

The code currently in your question looks like if it were written for a procedural language - in particular the use of list-ref for this kind of problem is not quite right. You have to stop thinking in C/C++/C#/Java or whatever your usual programming language is, and start thinking in the Scheme way - which favors a more functional-style of programming.

Upvotes: 3

Related Questions