From: Mark Carter
Subject: Functions that create functions
Date: 
Message-ID: <slrndgriul.q9t.me@dana.freedns.us>
n00b alert:

I am trying to create a function, which I want to put into
a let statement eventually. As an experiment, I tried to
do this outwith a function definition first.

So I type out the code:
(setq f1 (lambda (x) ( / 1 x )))
(f1 10)

and get back that the function f1 is undefined. Bummer. I'm
following the example on:
http://simon.incutio.com/archive/2002/10/03/lispSpecialForms
so it should work(!)

Anyone care to put me out of my misery?

From: Eric Lavigne
Subject: Re: Functions that create functions
Date: 
Message-ID: <1124978612.806509.298900@f14g2000cwb.googlegroups.com>
Mark Carter wrote:
> n00b alert:
>
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.
>
> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)
>
> and get back that the function f1 is undefined. Bummer. I'm
> following the example on:
> http://simon.incutio.com/archive/2002/10/03/lispSpecialForms
> so it should work(!)
>
> Anyone care to put me out of my misery?

I haven't tested this, but my understanding is that f1 is not a
function but rather an ordinary variable that stores a function. In
that case, you could replace (f1 10) with (funcall f1 10)
From: Paul F. Dietz
Subject: Re: Functions that create functions
Date: 
Message-ID: <FOudnZF0H4pEUpDeRVn-1Q@dls.net>
Mark Carter wrote:
> n00b alert:
> 
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.
> 
> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)
> 
> and get back that the function f1 is undefined. Bummer. I'm
> following the example on:
> http://simon.incutio.com/archive/2002/10/03/lispSpecialForms
> so it should work(!)
> 
> Anyone care to put me out of my misery?

The example on that page is wrong.

You need to do (funcall f1 10).

This gets talked about at least twice a month on this group.
The short explanation is that Common Lisp has (at least) two
namespaces: one for variables, and one for functions.  Putting
a symbol in the car of a form, as you did, accesses the
function namespace; one uses funcall to call a functional
value that is the value of an ordinary expression, such
as a variable.

Common Lisp is not Scheme; you can't put an arbitrary
expression in the car of a form and use its value as the
function to be called.  Scheme has only the one namespace.

Search on Lisp-2 and Lisp-1 in the archives for discussions
of this issue, and why some prefer Common Lisp's approach.

	Paul
From: Pascal Costanza
Subject: Re: Functions that create functions
Date: 
Message-ID: <3n61e6F1c7eU1@individual.net>
Mark Carter wrote:
> n00b alert:
> 
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.
> 
> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)
> 
> and get back that the function f1 is undefined. Bummer.

Try (funcall f1 10).

Functions and other values are by default stored in different 
namespaces. You are not using the default way to define a function, but 
instead have chosen to store a function in the value namespace. funcall 
is used to "shift" a function from the value namespace and call it like 
a function.

The default way is to use defun and that stores a function in the 
function namespace. This especially means that a name can be bound to a 
function and another ordinary value at the same time, which can be 
useful. That's the reason why Common Lisp is sometimes called a Lisp-2 - 
the "2" stands for "two namespaces".

Sidenote: Using setq on an undefined variable is not defined in ANSI 
Common Lisp and yields implementation-specific behavior. Be careful to 
use this only for experiments.


Pascal

-- 
OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol
++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++
From: Mark Carter
Subject: Re: Functions that create functions
Date: 
Message-ID: <slrndgrlqq.16t.me@dana.freedns.us>
On 2005-08-25, Pascal Costanza <··@p-cos.net> wrote:
> Mark Carter wrote:
>> n00b alert:
>> 
>> I am trying to create a function, which I want to put into
>> a let statement eventually. As an experiment, I tried to
>> do this outwith a function definition first.
>> 
>> So I type out the code:
>> (setq f1 (lambda (x) ( / 1 x )))
>> (f1 10)
>> 
>> and get back that the function f1 is undefined. Bummer.
>
> Try (funcall f1 10).
>
> Functions and other values are by default stored in different 
> namespaces. You are not using the default way to define a function, but 
> instead have chosen to store a function in the value namespace. funcall 
> is used to "shift" a function from the value namespace and call it like 
> a function.
>

Thanks.

Actually, I was going to loose heart about that, because I thought it
would
mean that my functions would look nastey. I had already defined the
function:
(defun norm (a)
    "normalise an array/list A so that the sum of components is 1"
    (let ((inv (invsuml a)))
        (mapcar #'(lambda (x) (* x inv)) a)))
but thought it looked horrid. Dunno, my sense of aethetics didn't
like all that lambda stuff, and all those parentheses.

But then I tried

(defun norm (a)
    "normalise an array/list A so that the sum of components is 1"
    (defun f (x) (* x (invsum a) ))
    (mapcar #'f a))
and it worked. This pleases me, because it makes the whole thing look
neater to me.

My experiments continue.

I'm currently reading Practical Common Lisp, and have made it to
chapter 3 so far. I like the way he explains things, which is much
nicer than what I saw in the Hyperspec, for example:
let ({var | (var [init-form])}*) declaration* form* => result*

I'm currently on a trial version of Corman Lisp. I like the way
you can set up a file to store your code, and then use Shift+Enter
to have it execute my most recent definition. 
If I'm sufficiently enthused by the whole lisp
thang, then I may be tempted to splash out on the full license; 
although of course, being for personal use as a hobby, it requires
a bit of humming and harring on my part.
From: Pascal Costanza
Subject: Re: Functions that create functions
Date: 
Message-ID: <3n64csF1rauU1@individual.net>
Mark Carter wrote:

> Actually, I was going to loose heart about that, because I thought it
> would
> mean that my functions would look nastey. I had already defined the
> function:
> (defun norm (a)
>     "normalise an array/list A so that the sum of components is 1"
>     (let ((inv (invsuml a)))
>         (mapcar #'(lambda (x) (* x inv)) a)))
> but thought it looked horrid. Dunno, my sense of aethetics didn't
> like all that lambda stuff, and all those parentheses.
> 
> But then I tried
> 
> (defun norm (a)
>     "normalise an array/list A so that the sum of components is 1"
>     (defun f (x) (* x (invsum a) ))
>     (mapcar #'f a))
> and it worked. This pleases me, because it makes the whole thing look
> neater to me.

This may work as expected, but has effects that you probably don't want: 
The nested defun actually defines f globally, which means that you can 
call f even outside of norm as soon as norm has been executed at least once.

If you want to restrict the definition to the local scope, use flet or 
labels:

(defun norm (a)
   (flet ((f (x) (* x (invsum a))))
     (mapcar #'f a)))

> If I'm sufficiently enthused by the whole lisp
> thang, then I may be tempted to splash out on the full license; 

Then you should start to put some money aside... ;)


Pascal

-- 
OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol
++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++
From: Thomas A. Russ
Subject: Re: Functions that create functions
Date: 
Message-ID: <ymi3bosfwvn.fsf@sevak.isi.edu>
Mark Carter <··@privacy.net> writes:

> But then I tried
> 
> (defun norm (a)
>     "normalise an array/list A so that the sum of components is 1"
>     (defun f (x) (* x (invsum a) ))
>     (mapcar #'f a))
> and it worked. This pleases me, because it makes the whole thing look
> neater to me.

OK, but you have to realize that the DEFUN inside of NORM is a global
definition of the function F.  (At least in Common Lisp).  If you want
to have a local, lexical definition, you will need to use an FLET or
LABELS form.  They are similar, except that the scope rules for the
names differs.  Operationally, that means you can only use FLET for
non-recursive functions and need LABELS for recursive functions.  In
your example, this would look like:

(defun norm (a)
   "normalise an array/list A so that the sum of components is 1"
   (flet ((f (x) (* x (invsum a) )))
     (mapcar #'f a)))

> My experiments continue.
> 
> I'm currently reading Practical Common Lisp, and have made it to
> chapter 3 so far. I like the way he explains things, which is much
> nicer than what I saw in the Hyperspec, for example:
> let ({var | (var [init-form])}*) declaration* form* => result*

Well, the Hyperspec is the language SPECIFICATION, and not a tutorial,
so it doesn't come as much of a surprise that learning the language from
the formal specification is more difficult.  Choosing a instructional
book is clearly the superior way to go.

By the way, I would also recommend Peter Norvig's excellent "Paradigms
of AI Programming:  Case Studies in Common Lisp" as a wonderful book to
show you how to use Common Lisp to do cool stuff.  This book is
sufficiently well-known in the community that it often is referred to
by its initials: PAIP.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Ulrich Hobelmann
Subject: Re: Functions that create functions
Date: 
Message-ID: <3n639bF1psaU1@individual.net>
Mark Carter wrote:
> n00b alert:
> 
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.
> 
> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)

A "let" statement would be:
(flet ((f1 (x) (/ 1 x)))
   (f1 10))

That way you also avoid having to use funcall, because flet establishes 
a function binding, not a value binding.

-- 
I believe in Karma.  That means I can do bad things to people
all day long and I assume they deserve it.
	Dogbert
From: Mark Carter
Subject: Re: Functions that create functions
Date: 
Message-ID: <slrndgrn0f.r3e.me@dana.freedns.us>
On 2005-08-25, Ulrich Hobelmann <···········@web.de> wrote:
> Mark Carter wrote:
>> n00b alert:
>> 
>> I am trying to create a function, which I want to put into
>> a let statement eventually. As an experiment, I tried to
>> do this outwith a function definition first.
>> 
>> So I type out the code:
>> (setq f1 (lambda (x) ( / 1 x )))
>> (f1 10)
>
> A "let" statement would be:
> (flet ((f1 (x) (/ 1 x)))
>    (f1 10))
>
> That way you also avoid having to use funcall, because flet establishes 
> a function binding, not a value binding.

Aaaah. Many thanks.

PS not sure why my previous post appears as a response to you post.
 Still, never mind.
>
From: http://public.xdi.org/=pf
Subject: Re: Functions that create functions
Date: 
Message-ID: <m2slwyqe3y.fsf@mycroft.actrix.gen.nz>
On 25 Aug 2005 13:47:01 GMT, Mark Carter wrote:

> n00b alert:
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.

> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)

> and get back that the function f1 is undefined. Bummer. I'm
> following the example on:
> http://simon.incutio.com/archive/2002/10/03/lispSpecialForms
> so it should work(!)

> Anyone care to put me out of my misery?

In Common Lisp, function names and variable names are different.  You
defined a /variable/ named F1, and then tried to call a /function/
named F1 -- which doesn't exist; hence the error.

You need to write (funcall f1 10), not just (f1 10).  Or else write

  (defun f1 (x) (/ x))   ; note that (/ 1 x) == (/ x)

to define a function, and then (f1 10) will work.

-- 
Don't worry about people stealing your ideas. If your ideas are any good,
you'll have to ram them down people's throats.
                                                          -- Howard Aiken
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: =?iso-8859-1?B?TWFudWVsIEj2bN8=?=
Subject: Re: Functions that create functions
Date: 
Message-ID: <1125093342.913525.97390@g43g2000cwa.googlegroups.com>
Maybe, you want to have a look at section 2.5 of Paul Grahams "on
lisp".
Greetings
From: elof
Subject: Re: Functions that create functions
Date: 
Message-ID: <pan.2005.08.26.15.01.52.964250@image.dk>
Den Thu, 25 Aug 2005 13:47:01 +0000. skrev Mark Carter:

> n00b alert:
> 
> I am trying to create a function, which I want to put into
> a let statement eventually. As an experiment, I tried to
> do this outwith a function definition first.
> 
> So I type out the code:
> (setq f1 (lambda (x) ( / 1 x )))
> (f1 10)
> 
> and get back that the function f1 is undefined. Bummer. I'm
> following the example on:
> http://simon.incutio.com/archive/2002/10/03/lispSpecialForms
> so it should work(!)
> 
> Anyone care to put me out of my misery?

;; putting a lambda into the function slot that f1 is bound to at the
moment 
CL-USER> (setf (symbol-function 'f1) (lambda (x) (/ 1 x)))
#<Interpreted Function (LAMBDA (X) (/ 1 X)) {5BFC0931}> 

;; Evaluating f1 as a function,
CL-USER> (f1 2)
1/2

;;putting a lambda into the variable slot that f1 is bound to at the
moment 
CL-USER> (setf f1 (lambda (x) (/ 1 x))) 
#<Interpreted Function (LAMBDA (X) (/ 1 X)) {5BFA7AD9}> 

;; using f1 as a variable
CL-USER> f1 
#<Interpreted Function (LAMBDA (X) (/ 1 X)) {5BFA7AD9}>

;; calling the function funcall with the identifier f1 as an
argument. Funcall will retreive and execute the function that f1 is bound
to CL-USER> (funcall f1 2)
1/2


The thing to understand is that an identifier can point to both a function
and a variable at the same time. So you can do stuff like this, if you
like to confuse people who have to read your code ;-)

CL-USER> (let ((let 42))
	   let)
42
CL-USER> (defun plus-2 (plus-2)
	   (+ 2 plus-2))
PLUS-2
CL-USER> (plus-2 2)
4

	Kristian