From: ·········@math.ufl.edu
Subject: Passing a function-selector into a function
Date: 
Message-ID: <1191525594.129785.17810@o80g2000hse.googlegroups.com>
I'm trying to figure out a way to pass a fnc-selector into
a fnc `foo' so that the selected-fnc is dispatched efficiently.

At the same time, I'm trying to learn how `flet' and `macrolet' work.

Invoking foo(3), I'd like `foo' to use its third fnc.  This
seems to work:

    (defun foo (selector)
      (let ((fnc (case selector  ... truefnc1 ... truefnc2 ....))
	    )
	(loop repeat LOTSATIMES  do
	      (funcall fnc <args>) )
	) )

Is there some way to do this with `flet'?  I believe that formulation

    (defun foo (selector)
      (flet ((fnc (<args>) (case selector  ...))
	     )
	(loop repeat LOTSATIMES  do
	      (fnc <args>) )
	) )

calls `case' upon each invocation of `fnc', which is poor.

I suppose I could wrap the entire `flet' inside of a
backquote, inside of a `let', but perhaps someone can
suggest an easier way.  One reason that I'm asking about
`flet' is this:

   In interpreted code, is an invocation `(fnc <args>)',
   [with `fnc' bound by `flet'] dispatched faster than
   `(funcall fnc <args>)' [with `fnc' bound by `let'], or
   is this simply implementation dependent?  --same
   question for compiled code.

Going back to the selector question, I also considered

    (defun foo (selector)
      (macrolet ((fnc (<args>)
		      (case selector
			...   ` (truefnc1 ...)  ;! Backquoted expression.
			...   ` (truefnc1 ...)  ;! Backquoted expression.
			) ))
	(loop repeat LOTSATIMES  do
	      (fnc <args>) ) ) )

Alas, the occurrence of `selector' in the `case' statement is
not visible to the `macrolet', as the hyperspec indeed states.

--
Prof. Jonathan LF King	 Mathematics dept, Univ. of Florida
            <http://www.math.ufl.edu/~squash/teaching.html>
From: Rainer Joswig
Subject: Re: Passing a function-selector into a function
Date: 
Message-ID: <joswig-66066D.21320404102007@news-europe.giganews.com>
In article <·······················@o80g2000hse.googlegroups.com>,
 ·········@math.ufl.edu wrote:

> I'm trying to figure out a way to pass a fnc-selector into
> a fnc `foo' so that the selected-fnc is dispatched efficiently.
> 
> At the same time, I'm trying to learn how `flet' and `macrolet' work.
> 
> Invoking foo(3), I'd like `foo' to use its third fnc.  This
> seems to work:
> 
>     (defun foo (selector)
>       (let ((fnc (case selector  ... truefnc1 ... truefnc2 ....))
> 	    )
> 	(loop repeat LOTSATIMES  do
> 	      (funcall fnc <args>) )
> 	) )

this is fine

> 
> Is there some way to do this with `flet'?  I believe that formulation
> 
>     (defun foo (selector)
>       (flet ((fnc (<args>) (case selector  ...))
> 	     )
> 	(loop repeat LOTSATIMES  do
> 	      (fnc <args>) )
> 	) )
> 
> calls `case' upon each invocation of `fnc', which is poor.

right.

(defun foo (selector)
  (let ((function-object (case selector  ...)))
    (flet ((fnc (<args>)
        (funcall function-object <args>)))
      (loop repeat LOTSATIMES
            do (fnc <args>)))))


> 
> I suppose I could wrap the entire `flet' inside of a
> backquote, inside of a `let', but perhaps someone can
> suggest an easier way.  One reason that I'm asking about
> `flet' is this:
> 
>    In interpreted code, is an invocation `(fnc <args>)',
>    [with `fnc' bound by `flet'] dispatched faster than
>    `(funcall fnc <args>)' [with `fnc' bound by `let'], or
>    is this simply implementation dependent?  --same
>    question for compiled code.
> 
> Going back to the selector question, I also considered
> 
>     (defun foo (selector)
>       (macrolet ((fnc (<args>)
> 		      (case selector
> 			...   ` (truefnc1 ...)  ;! Backquoted expression.
> 			...   ` (truefnc1 ...)  ;! Backquoted expression.
> 			) ))
> 	(loop repeat LOTSATIMES  do
> 	      (fnc <args>) ) ) )
> 
> Alas, the occurrence of `selector' in the `case' statement is
> not visible to the `macrolet', as the hyperspec indeed states.
> 
> --
> Prof. Jonathan LF King	 Mathematics dept, Univ. of Florida
>             <http://www.math.ufl.edu/~squash/teaching.html>

-- 
http://lispm.dyndns.org