I am just trying to randomly make a list and use it in a larger function.
(define make-random-list
(if
(= (random 2) 0) (list 2 3)
(list 3 2)))
This only produces the list (2 3) and I am not sure why. What is happening to cause this?
I can make the function work if I write it like this
(define make-random-list
(lambda (x)
(if
(= (random x) 0) (list 2 3)
(list 3 2))))
and calling (make-random-list 2)
but I do not understand why that would work and the other one would not. What is going on with scheme that would not allow the first function to produce random results?
In your first snippet, you’re assigning the result of a one-time computation to a variable. What you need here is to define a function that will be evaluated every time it’s invoked. Your second snippet does exactly that, but there is a shorter way to express the same:
Note the difference in the syntax: a function definition encloses the function definition together with the formal argument names in parentheses, while there are no parentheses around the name of a variable.