Random function in the circuit

I am trying to get a random string from a list of strings in a schema. List of examples ("this" ", which is" today "yesterday") Therefore, based on the length of the list, a random number is created and this word is displayed. But keep getting bugs!

I tried like this:

;; produces random number that should be input to the random-function

(define (random-num list)

(random-function ((random (length (list))) list)))

;; loops the number of times till random number is 0 and outputs the list value

(define (random-function num list )
  (cond 
    [(zero? num) (car list)]
    [else (random-function (- num 1) (cdr list))]))

Error:

procedure application: expected procedure, given: 
("this" "that" "today" "yesterday") (no arguments)

When I try to do:

(random-function (random (length list)) 

on the console, I get a random number.

I don’t understand why it crashes here when done inside my program.

Can I do it better and not cycle so many times. In Java, I would use an array and set the position directly. In any case, to do this in the circuit?

+3
source share
1 answer
(define (random-element list)
  (list-ref list (random (length list))))
+10
source

Source: https://habr.com/ru/post/1774728/


All Articles