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.
You can easily solve this one by using
map:Or even shorter:
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:
Or even shorter:
Either way, call it like this:
The code currently in your question looks like if it were written for a procedural language – in particular the use of
list-reffor 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.