help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Defining functions on the fly


From: Gene
Subject: Re: Defining functions on the fly
Date: Tue, 16 Jun 2015 17:24:18 -0700 (PDT)
User-agent: G2/1.0

On Monday, June 15, 2015 at 9:14:56 PM UTC-4, Stefan Monnier wrote:
> > (fset (function name-of-function)
> [...]
> > (define name-of-function
> 
> Actually, `fset' is more like Scheme's `set!',

How so?
`define' is typically used if or when a symbol has yet to be bound to either a 
lambda function or value.
set! is used to destructively re-bind something to a symbol already bound to 
something.

> so if it's the *definition* of a function, you should use `defalias' 
> which is more like Scheme's `define'.

I don't see it that way, but if works to one's satisfaction then why not?

I've taken to developing functions via lambda, then -- when I've got things 
hammered out the way I want them I use fset -- typically insight a wrapper in 
which the name of the function is available as, say, `func-name' to 
programmatically name the previously anonymous function.

(let ; function forge
(
 (func-name 
  (quote 
    non-anon ; arbitrary name.  We could have used `gensym' to have emacs 
generate a symbol for us
 ))
)

(fset func-name
 (lambda
  (
   arg1 ; we can document the arg name this way
   arg2 ; This is a seperate line to document this arg
  )
  "docstring"
   (list arg1 arg2)
  );lambda
 );fset
 
 ;
  (if (functionp func-name) 
   (symbol-function func-name)
   (message (concat "function " func-name " not defined"))
  )

) ;let

> 
> 
>         Stefan

I tend to use defalias for functions which already have names.
When I'm dreaming up a new name which doesn't collide with an extant named 
function known to function-name space I have no problem with using fset as if 
scheme's `define' as per a pristine, virgin binding.

Gene


reply via email to

[Prev in Thread] Current Thread [Next in Thread]