Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Constructing array inside function [newbie]
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeremy Whetzel  
View profile  
 More options Mar 8 2002, 8:47 am
Newsgroups: comp.lang.lisp
From: Jeremy Whetzel <li...@toadmail.com>
Date: 08 Mar 2002 08:49:52 -0500
Local: Fri, Mar 8 2002 8:49 am
Subject: Constructing array inside function [newbie]

[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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stig Hemmer  
View profile  
 More options Mar 8 2002, 9:53 am
Newsgroups: comp.lang.lisp
From: Stig Hemmer <s...@pvv.ntnu.no>
Date: 08 Mar 2002 15:53:53 +0100
Local: Fri, Mar 8 2002 9:53 am
Subject: Re: Constructing array inside function [newbie]
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 10:59 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Fri, 08 Mar 2002 15:59:16 GMT
Local: Fri, Mar 8 2002 10:59 am
Subject: Re: Constructing array inside function [newbie]
* 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nils Goesche  
View profile  
 More options Mar 8 2002, 11:11 am
Newsgroups: comp.lang.lisp
From: Nils Goesche <car...@cartan.de>
Date: 8 Mar 2002 16:11:15 GMT
Local: Fri, Mar 8 2002 11:11 am
Subject: Re: Constructing array inside function [newbie]

In article <3224591965937...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 12:50 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Fri, 08 Mar 2002 17:50:05 GMT
Local: Fri, Mar 8 2002 12:50 pm
Subject: Re: Constructing array inside function [newbie]
* 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.

///
--
  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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stig Hemmer  
View profile  
 More options Mar 8 2002, 4:24 pm
Newsgroups: comp.lang.lisp
From: Stig Hemmer <s...@pvv.ntnu.no>
Date: 08 Mar 2002 22:24:27 +0100
Local: Fri, Mar 8 2002 4:24 pm
Subject: Re: Constructing array inside function [newbie]
Thank you for not naming me.

Stig Hemmer,
Jack of a Few Trades.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Whetzel  
View profile  
 More options Mar 8 2002, 10:31 pm
Newsgroups: comp.lang.lisp
From: Jeremy Whetzel <li...@toadmail.com>
Date: 08 Mar 2002 22:34:05 -0500
Local: Fri, Mar 8 2002 10:34 pm
Subject: Re: Constructing array inside function [newbie]

Erik Naggum <e...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Coby Beck  
View profile  
 More options Mar 9 2002, 2:34 am
Newsgroups: comp.lang.lisp
From: "Coby Beck" <cb...@mercury.bc.ca>
Date: Sat, 09 Mar 2002 07:34:28 GMT
Local: Sat, Mar 9 2002 2:34 am
Subject: Re: Constructing array inside function [newbie]

"Erik Naggum" <e...@naggum.net> wrote in message

news:3224591965937406@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")


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Coby Beck  
View profile  
 More options Mar 9 2002, 2:36 am
Newsgroups: comp.lang.lisp
From: "Coby Beck" <cb...@mercury.bc.ca>
Date: Sat, 09 Mar 2002 07:36:02 GMT
Local: Sat, Mar 9 2002 2:36 am
Subject: Re: Constructing array inside function [newbie]

"Stig Hemmer" <s...@pvv.ntnu.no> wrote in message

news:ekvu1rr860u.fsf@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]

these last two suggestions strike me as very bad advice.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
synthespian  
View profile  
 More options Mar 9 2002, 9:32 pm
Newsgroups: comp.lang.lisp
From: synthespian <synthesp...@uol.com.br>
Date: Sat, 09 Mar 2002 21:26:00 -0300
Subject: Re: Constructing array inside function [newbie]

In article <3224591965937...@naggum.net>, Erik Naggum <e...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »