Josh
Josh

Reputation: 519

Local in scheme

I am just making up random programs and looking if I can use local. Is there a way I can use local for this? -

(define (filt n)
  (filter number? n))

(define (mapn n)
 (map add1 (filt n)))

(define (mapl n)
  (map list (mapn n)))

    (check-expect (mapl(list 1 2 false "true" "b" 'b true 4 9)) (list (list 2) (list 3) (list 5) (list 10)))

The first program makes sure the output are only numbers. The second one adds 1 to each number, and the third program creates a list of each individual element inside the list.

I tried but I get no answer and end up with a user break.

(define (mapit n)
  (local [(define (filt l)
          (filter number? l))]
    (local [(define (mapn b)
             (map add1 b))]
      (mapit (map list n)))))

Upvotes: 0

Views: 66

Answers (1)

daniel gratzer
daniel gratzer

Reputation: 53871

You want to use let.

(let ( (name value)
       (othername othervalue))
  expression-using-name)

So

(define (map-it n)
  (let ((filt (lambda (l)
                 (filter number? l)))
        (mpan (lambda (b)
                 (map add1 b))))
     (mapit map list n)))

But what are you trying to accomplish here? You're not actually using mapn or filt anywhere.

I think you can just use

(define (mapl n)
  (define (filt n)
    (filter number? n))

  (define (mapn n)
   (map add1 (filt n)))

  (map list (mapn n)))

Since scheme allows nested definitions.

Upvotes: 1

Related Questions