2 Replies - 818 Views - Last Post: 15 July 2016 - 11:57 AM

#1 KidzKlub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 16

question about using lambda as argument in a procedure

Posted 15 July 2016 - 11:23 AM

This is a very beginner question as I just started learning yesterday with zero background.

I'm using the MIT opencourseware for Computer Science 6.001. I'm also using repl.it to replicate what is happening in the lectures. In the second lecture he is teaching how to approximate pi/8 with sigma notation. First we define sum, and then we use it to apply to all kinds of sigma notations. My problem arises when trying to use lambda as an argument in order to denote a procedure. I'm typing exactly what the instructor does, but in repl.it the lambda is greyed out and I get an error saying that [pi-sum] is not a function. I also found a much simpler use of lambda as an argument online, if you don't feel like analyzing the sum procedure. Here is the code:

 
(define (1+ x) (+ 1 x))
(define (sum term a next B)/>/>
	(if (> a B)/>/>
	    0
	    (+ (term a)
	       (sum term
	            (next a)
	            next
	            B)/>/>)))
(define (pi-sum a B)/>/>
	(sum (lambda (i) (/ 1 (* i (+ i 2))))
	     a
	     (lambda (i) (+ i 4))
	     B)/>/>)



(define (add-three-to-each sent)
	(every (lambda (number) (+ number 3)) sent))



Neither of these work when I try to evaluate them. In both instances lambda is greyed out in repl.it. I tried using another compiler online and got the same results. Please help.

I forgot to add that this is using the scheme dialect of LISP. My apologies.

Is This A Good Question/Topic? 0
  • +

Replies To: question about using lambda as argument in a procedure

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,904
  • Joined: 12-December 12

Re: question about using lambda as argument in a procedure

Posted 15 July 2016 - 11:48 AM

There is a problem with the editor that inserts those /> symbols. I've removed them by placing a space between b and )

(define (1+ x) (+ 1 x))
(define (sum term a next b )
    (if (> a b )
        0
        (+ (term a)
           (sum term
                (next a)
                next
                b ))))
(define (pi-sum a b )
    (sum (lambda (i) (/ 1 (* i (+ i 2))))
         a
         (lambda (i) (+ i 4))
         b ))

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,904
  • Joined: 12-December 12

Re: question about using lambda as argument in a procedure

Posted 15 July 2016 - 11:57 AM

That code runs for me in DrRacket so I cannot help further. Actually, it runs for me in repl.it as well(?).

Attached Image

(I don't know what numbers you are supposed to supply it.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1