Declare lambda inside a loop?

Discussion of Common Lisp
Post Reply
wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Declare lambda inside a loop?

Post by wvxvw » Sun May 08, 2011 5:00 am

Hello, I'm curious about whether it is possible, and if so, then how would I declare a lambda within a loop. Here's an example I tried. It compiled, but gives a runtime error, saying it didn't bind lambda to the variable.

Code: Select all

(defun handle-incoming (stream)
	(loop 	for char = (code-char (read-byte stream nil 0))
		with handled = nil
		with message = nil
		with write-response = #'(lambda () 
			(when word (setf message (append message (list word)))) nil)
		for word = (if (or (char= char +null+) (char= char +pipe+))
			(write-response) ; This will look for a function with the name "write-response" elsewhere
			; while I wanted it to call the lambda declared before in this loop
			(progn (setf handled nil)
				(concatenate 'string word (string char))))
		when (and (char= char +null+) (not handled))
		do (progn (write-response)
			(funcall 'call-handler message stream)
			(setf handled t))))
Sorry, that's not what you'd call a clean code... but, hope, it explains what I'm looking for

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Declare lambda inside a loop?

Post by ramarren » Sun May 08, 2011 7:41 am

"Declare a lambda" is not a proper terminology. I believe you mean "bind an anonymous function to a variable". You can call functions bound to variables with FUNCALL. The first argument is a function designator, which typically would be either a variable bound to a function of a function producing function. Using it on a constant symbol, as you do, works, but is redundant, since it is equivalent to standard function call.

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Declare lambda inside a loop?

Post by wvxvw » Sun May 08, 2011 9:07 am

Yes, it did work.
Thanks for ongoing help :)

Post Reply