Josh
Josh

Reputation: 519

Using local in scheme

(define-struct point (x y))


(define (helper lon)
  (* (point-x lon)
     (point-y lon)))

(define (cart lon)

  (cond
    [(empty? lon) 0]
    [else
     (+ (helper (first lon))
        (cart (rest lon))1)]))

I am just playing around, making a bunch of functions to see if and where I can use local. This just multiplies point x and y and adds 1 to the result. Is there a way I can replace the helper function I created here and use local?

Upvotes: 0

Views: 49

Answers (1)

Óscar López
Óscar López

Reputation: 235994

Sure, this seems like a good place for using local, as long the helper procedure is only being used inside cart:

(define (cart lon)
  (local [(define (helper lon)
            (* (point-x lon)
               (point-y lon)))]
    (cond
      [(empty? lon) 0]
      [else
       (+ (helper (first lon))
          (cart (rest lon))
          1)])))

Also notice that, depending on the language in use, local might not be necessary, a simple internal definition will do the trick, too:

(define (cart lon)
  (define (helper lon)
    (* (point-x lon)
       (point-y lon)))
  (cond
    [(empty? lon) 0]
    [else
     (+ (helper (first lon))
        (cart (rest lon))
        1)]))

Upvotes: 1

Related Questions