I am currently completing chapter 7 of the htdp book and am trying to create an instance of the spider structure:
#lang racket
;; spider-structure: structure -> ???
;; defines a spider structure with two parameters: legs and volume
(define-struct spider (legs volume))
;; spidercheck: lambda -> num
;; creates a spider check function and determines volume based on legs
(define spidercheck
(lambda (legs)
(cond
((<= legs 4) 800)
((> legs 4) 1000))))
(define a-spider
(make-spider 4
(spidercheck ...
My problem is that I want to pass the number from (make-spider 4) to the spidercheck function within the a-spider function. I have tried (spider-legs a-spider) but of course it says it is used before its definition. Any help is appreciated.
Thanks!
A simplistic solution would be to call
make-spiderandspidercheckwith the same parameter, let's say the number2:A more interesting alternative would be to define a new function that enforces the restriction that the same
n, the number of legs, is passed as parameter for bothmake-spiderandspider check:Now, whenever you want to create a spider you can specify just the number of legs as parameter, and let
spiderchecktake care of calculating the volume. Also notice that you can simplify the above snippet even more, using thedefinesyntax that makes thelambdaimplicit:Either way, to create a new spider (for instance, with
8legs) do this: