Making a random list in Scheme

…衆ロ難τιáo~ 提交于 2019-12-24 07:54:49

问题


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?


回答1:


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:

(define (make-random-list x)
(if
  (= (random x) 0) (list 2 3)
                   (list 3 2)))

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.




回答2:


In Scheme, functions are defined by explicitly using a lambda like this:

(define square
  (lambda (x)
    (* x x)))

Or like this, which is shorthand for the previous syntax and implicitly is using a lambda under the hood - both flavors of procedure definition are completely equivalent:

(define (square x)
  (* x x))

The first version of your code wasn't working because this is just assigning the result of the evaluation of if to a variable called make-random-list:

(define make-random-list
  (if (= (random 2) 0)
      (list 2 3)
      (list 3 2)))

For defining a procedure in Scheme you have to use a lambda, either explicitly or implicitly. So your procedure should be defined using either of this equivalent forms:

(define make-random-list
  (lambda ()
    (if (= (random 2) 0)
        (list 2 3)
        (list 3 2)))

(define (make-random-list)
  (if (= (random 2) 0)
      (list 2 3)
      (list 3 2)))

Notice that you don't have to pass the x parameter if the only possible value is 2, simply declare a procedure with no arguments.



来源:https://stackoverflow.com/questions/13555956/making-a-random-list-in-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!