Replacing list '(RANDOM) with a random number between 0 and 100 in scheme

Viewed 49

My current code for this program is

(define (my-equal? x y)
  (if (eq? x y)  ; this takes care of atoms
      #t
      (if (or (or (null? x) (null? y))  ; false if either is '()
              (not (and (list? x) (list? y))))  ; false if both are not lists
          #f
          (if (and (list? (car x)) (list? (car y)))
              (and (my-equal? (car x) (car y))
                   (my-equal? (cdr x) (cdr y)))
              (and (eq? (car x) (car y))
                   (my-equal? (cdr x) (cdr y)))))))

(define (tsar subj srch repl)
  (if (null? subj)
      subj
      (if (equal? srch (car subj)) 


        ; NEED to put something between these lines to search for random,
        ; or attach it somehow.
          (cons repl (tsar (cdr subj) srch repl))

          (if (list? (car subj))
              (cons (tsar (car subj) srch repl) (tsar (cdr subj) srch repl))
              (cons (car subj) (tsar (cdr subj) srch repl))))))

(display (tsar '(x (x) z) 'x '(y y)))
(newline)
(newline)
(display (tsar '(x (x) z) '(x) '(y y)))
(newline)
(newline)
(display (tsar '(x () z) '() 'y))
(newline)
(newline)
(display (tsar ’(x (x) ((x)) z) ’(x) ’(RANDOM y)))

This function takes a subject subj as a parameter which is typically a list, a value to search for in the list srch and a value to replace the value with repl

Here are a few expected outcomes:

(tsar ’(x (x) ((x)) z) ’(x) ’()) yields: (x () (()) z)

(tsar ’(x (x) ((x)) z) ’() ’(y y)) yields: (x (x y y) ((x y y) y y) z y y)

(tsar ’(x (x) ((x)) z) ’(x) ’(RANDOM y)) yields: (x (74 y) ((46 y)) z)

What I'm having trouble implementing is when the program finds RANDOM in a list, how do I get it to output a random number from 0-100?

0 Answers
Related