Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Constructing array inside function [newbie]

11 views
Skip to first unread message

Jeremy Whetzel

unread,
Mar 8, 2002, 8:49:52 AM3/8/02
to

[I'm extremely new to CL. Please excuse, but feel free to correct, any
inaccuracies in my terminology. Thanks.]

Hi,

I'm trying to create a function that, given a name of a new array as an
argument, creates a new array of a default length. This is so that I
can use one function to easily setup the array initially, and then I
want to create other functions that abstract the process of editing the
various elements inside of the array. Here is the code I'm
using:

(defun create-array(new-array)
(setf new-array (make-array 4 :initial-element 'blank))
(print new-array))

Then I call it using:

(create-array 'name-of-array)

When the 'print' fires from inside the function, everything looks fine.
But i I try this:

(print name-of-array)

I get the following:

*** - EVAL: variable NAME-OF-ARRAY has no value

I'm using CLISP. I'm not sure what I'm doing wrong. What I want is for
the newly created array to be accessable outside of the function. I
tried using defparameter, but that didn't seem to work either. Any
pointers are GREATLY appreciated.

Thanks,
Jeremy

Stig Hemmer

unread,
Mar 8, 2002, 9:53:53 AM3/8/02
to
Jeremy Whetzel <li...@toadmail.com> writes:
: (defun create-array(new-array)

: (setf new-array (make-array 4 :initial-element 'blank))
: (print new-array))
: (create-array 'name-of-array)
: (print name-of-array)
: *** - EVAL: variable NAME-OF-ARRAY has no value

The problem is that the SETF only modifies the local variable
NEW-ARRAY that disappears when the function returns.

This can be solved in several ways. You will have to judge for
yourself which solution fits your needs best.

The first solution is to make a function that returns an array with
the required properties:

: (defun create-array1 ()
: (make-array 4 :initial-element 'blank))
:
: (setf name-of-array (create-array1))
:
: (print name-of-array)

The second solution is to make a macro that does the job.

: (defmacro create-array2 (new-array)
: `(setf ,new-array (make-array 4 :initial-element 'blank)))
:
: (create-array2 name-of-array)
:
: (print name-of-array)

The third solution is to modify the symbol-table entry for this given
symbol. This is closest to the original, but generally not
considered "nice". However, if this is what you need...

: (defun create-array3 (new-array)
: (setf (symbol-value new-array)
: (make-array 4 :initial-element 'blank))
: (print new-array))
:
: (create-array3 'name-of-array)
:
: (print name-of-array)

Hope that helped.

Stig Hemmer,
Jack of a Few Trades.

PS: I haven't actually tested any of this.

Erik Naggum

unread,
Mar 8, 2002, 10:59:16 AM3/8/02
to
* Jeremy Whetzel <li...@toadmail.com>

| [I'm extremely new to CL. Please excuse, but feel free to correct, any
| inaccuracies in my terminology. Thanks.]

It seems that you are still writing in some other language.

| I'm trying to create a function that, given a name of a new array as an
| argument, creates a new array of a default length.

This is not how we do things in Common Lisp.

| This is so that I can use one function to easily setup the array
| initially, and then I want to create other functions that abstract the
| process of editing the various elements inside of the array.

The function should simply return the object you have created, and your
caller should bind it. The reason for this is that you cannot pass a
reference to any sub-object storage location into which your called
function can store the value. This is quite crucial to understand if you
are going to write Common Lisp in Common Lisp.



| (defun create-array(new-array)
| (setf new-array (make-array 4 :initial-element 'blank))
| (print new-array))
|
| Then I call it using:
|
| (create-array 'name-of-array)

I suggest you do this, instead:

(defun create-array (new-array)
(make-array 4 :initial-element 'blank))

and in your caller do

(let ((name-of-array (create-array)))
...)

or, if this is a global resource:

(defparameter *name-of-array* (create-array))

| I'm using CLISP. I'm not sure what I'm doing wrong.

Basically, you are still writing in some other language. You have to let
go of your desire to think you know what you are doing and start over as
a novice before you can actually know what you are doing.

| What I want is for the newly created array to be accessable outside of
| the function. I tried using defparameter, but that didn't seem to work
| either. Any pointers are GREATLY appreciated.

I hope the above is more helpful than receiving advice on how you should
accomplish what you should not want to do. Good luck learning Common
Lisp and unlearning bad habits from other languages. As you have seen
here in another response, not all those who think they know Common Lisp
have unlearned their old bad habits and may still think that helping
people with their explicit problem is better than trying to find and
solve their underlying problem.

///
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.

Nils Goesche

unread,
Mar 8, 2002, 11:11:15 AM3/8/02
to
In article <32245919...@naggum.net>, Erik Naggum wrote:
> I hope the above is more helpful than receiving advice on how you should
> accomplish what you should not want to do. Good luck learning Common
> Lisp and unlearning bad habits from other languages. As you have seen
> here in another response, not all those who think they know Common Lisp
> have unlearned their old bad habits and may still think that helping
> people with their explicit problem is better than trying to find and
> solve their underlying problem.

I guess you are referring to me. I was hoping the atrocious ugliness
of ``with-f-and-a'' was so glaring that it was the best way to
convince the OP in that thread that what he was trying to achieve
was wrong in the first place. :-)

Regards,
--
Nils Goesche
"The sooner all the animals are dead, the sooner we'll find
their money." -- Ed Bluestone
PGP key ID 0x42B32FC9

Erik Naggum

unread,
Mar 8, 2002, 12:50:05 PM3/8/02
to
* Nils Goesche <car...@cartan.de>

| I guess you are referring to me.

Not at all. I was referring to a fellow Norwegian who is permanently
clueless, yet very "helpful", so people get much further astray than they
were to begin with. With luck, you thought it was you because you have
been relieved of his "helpful" response. It is important to realize when
somebody would be hurt by being helped to achieve what they ask for, and
_not_ to hurt people who ask for help in hurting themselves. Some of the
clueless "good samaritans" out there never seem to learn this. Since
this particular cretin is one of those who go totally bananas when he is
not allowed to wear his halo after helping to hurt someone, I find it
vital to newsgroup peace not to name him, so at least he has a choice of
not responding and thus keeping the mystery of his identity instead of
making it obvious that he does not even understand when to shut up. So,
in summary, no, it was not you I referred to.

Stig Hemmer

unread,
Mar 8, 2002, 4:24:27 PM3/8/02
to
Thank you for not naming me.

Jeremy Whetzel

unread,
Mar 8, 2002, 10:34:05 PM3/8/02
to
Erik Naggum <er...@naggum.net> writes:

> It seems that you are still writing in some other language.

Now looking at it, yes, this seems to be the case.

> The function should simply return the object you have created, and your
> caller should bind it. The reason for this is that you cannot pass a
> reference to any sub-object storage location into which your called
> function can store the value. This is quite crucial to understand if you
> are going to write Common Lisp in Common Lisp.

I need to let this one maranade in my mind a bit. Being [fairly] new to
programming in general, and newer still to CL, it's still a bit to wrap
my head around, although the example helps quite a bit.

> I hope the above is more helpful than receiving advice on how you should
> accomplish what you should not want to do. Good luck learning Common
> Lisp and unlearning bad habits from other languages. As you have seen
> here in another response, not all those who think they know Common Lisp
> have unlearned their old bad habits and may still think that helping
> people with their explicit problem is better than trying to find and
> solve their underlying problem.

Sometimes it can be difficult to solve an underlying problem when one
doesn't realize there's a problem to begin with. Thanks for the
pointer. This is just the type of advice I need and was looking for.

Jeremy

Coby Beck

unread,
Mar 9, 2002, 2:34:28 AM3/9/02
to

"Erik Naggum" <er...@naggum.net> wrote in message
news:32245919...@naggum.net...
> * Jeremy Whetzel <li...@toadmail.com>

>
> | (defun create-array(new-array)
> | (setf new-array (make-array 4 :initial-element 'blank))
> | (print new-array))
> |
> | Then I call it using:
> |
> | (create-array 'name-of-array)
>
> I suggest you do this, instead:
>
> (defun create-array (new-array)
> (make-array 4 :initial-element 'blank))
>

No, you suggest he does this:

(defun create-array ()
(make-array 4 :initial-element 'blank))

(may not have been an obvious typo to the OP)

> and in your caller do
>
> (let ((name-of-array (create-array)))
> ...)
>

[etc]

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")


Coby Beck

unread,
Mar 9, 2002, 2:36:02 AM3/9/02
to

"Stig Hemmer" <st...@pvv.ntnu.no> wrote in message
news:ekvu1rr...@gnoll.pvv.ntnu.no...

> Jeremy Whetzel <li...@toadmail.com> writes:
> : (defun create-array(new-array)
> : (setf new-array (make-array 4 :initial-element 'blank))
> : (print new-array))
> : (create-array 'name-of-array)
> : (print name-of-array)
> : *** - EVAL: variable NAME-OF-ARRAY has no value
>
> The problem is that the SETF only modifies the local variable
> NEW-ARRAY that disappears when the function returns.
>
> This can be solved in several ways. You will have to judge for
> yourself which solution fits your needs best.
>
[snip good suggestion]

>
> The second solution is to make a macro that does the job.
>
> : (defmacro create-array2 (new-array)
> : `(setf ,new-array (make-array 4 :initial-element 'blank)))
> :
> : (create-array2 name-of-array)
> :
> : (print name-of-array)
>
> The third solution is to modify the symbol-table entry for this given
> symbol. This is closest to the original, but generally not
> considered "nice". However, if this is what you need...
>
> : (defun create-array3 (new-array)
> : (setf (symbol-value new-array)
> : (make-array 4 :initial-element 'blank))
> : (print new-array))
> :
> : (create-array3 'name-of-array)
> :
> : (print name-of-array)
>

these last two suggestions strike me as very bad advice.

synthespian

unread,
Mar 9, 2002, 7:26:00 PM3/9/02
to
In article <32245919...@naggum.net>, Erik Naggum <er...@naggum.net> wrote:
>
> You have to let
> go of your desire to think you know what you are doing and start over as
> a novice before you can actually know what you are doing.
>
>

The Zen of Lisp.

Cool!
I even printed this! :-)

Cheers
- henry

0 new messages