I am trying to get a random string from a list of strings in scheme. Example List 开发者_运维技巧( "this" "that" "today" "yesterday") So based on length of the list a random number is created and that word is output. But keep getting error!
I tried it 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.
Do not understand why it is crashing here when done inside my program???
Could I do this in a better way rather than looping so many times. In Java I would have used an array and given the position directly. Anyway to do it in scheme too ?
(define (random-element list)
(list-ref list (random (length list))))
精彩评论