I am trying to make a recursive function in Racket where from a list of colours, the function will print out multiple shapes in that order of colours. However, the function is not printing anything but "Empty List".
#lang slideshow
(require 2htdp/image)
(define a (list "red" "Orange" "Yellow" "Green" "Blue" "Purple"))
(define (Rainbow lst)
(cond
[(empty? lst) "Empty List"]
[else (circle 20 "solid" (first lst))
(Rainbow (rest lst))]))
(Rainbow a)
The displaying in REPL is done when a function returns an image. Your call (Rainbow a)
can be rewritten as sequence of (begin (circle 20 "solid" ...) ...)
( cond
has implicit begin, so in each recursive step, one begin
is added), finished with "Empty List"
and begin
returns last expression, so "Empty List"
is finally returned:
> (begin (circle 20 "solid" "Red")
(begin (circle 20 "solid" "Orange")
(begin (circle 20 "solid" "Yellow")
"Empty List")))
"Empty List"
But you can use print
and print that image into REPL:
#lang slideshow
(require 2htdp/image)
(define a (list "Red" "Orange" "Yellow" "Green" "Blue" "Purple"))
(define (Rainbow lst)
(cond
[(empty? lst) (void)]
[else (print (circle 20 "solid" (first lst)))
(Rainbow (rest lst))]))
(Rainbow a)
Note that I used void
, so my REPL contains only colored circles and no other text.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.