10 Replies - 5424 Views - Last Post: 09 February 2012 - 01:27 AM

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

adding values to arrays in Lisp

Posted 18 April 2011 - 05:41 PM

ok so i want to add a value to an array rather than a list. i can't seem to find a function in lisp that dose this however.
Is This A Good Question/Topic? 0
  • +

Replies To: adding values to arrays in Lisp

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: adding values to arrays in Lisp

Posted 18 April 2011 - 07:09 PM

Have you seen this:
Common Lisp Hints

* (defvar a)

A
* (setq a (make-array 3))

#(0 0 0)
* a

#(0 0 0)
* (aref a 1)

0
* (setf (aref a 1) 4)

4
* (aref a 1)

4


Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: adding values to arrays in Lisp

Posted 19 April 2011 - 12:56 PM

that adds values at an index rather than appending it to the array. i would like something that re-sized the array to add it in.
to be blunt i want to translate this code to lisp.
std::vector<int> a;
a.push_back(3);



if there was re-size function i could make my own push function for arrays. i found the function vector-push-extend(i was using vectors) but it apparently isn't defined by clisp by default.
Was This Post Helpful? 0
  • +
  • -

#4 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: adding values to arrays in Lisp

Posted 31 May 2011 - 06:49 PM

This is old, and you've probably moved past this, but I felt like trying to give you an answer anyway.

So, I think what you're asking for is taking an array, and adding something to it. Much like the 'append' function does to a list.

Ex. (append *list* '(1)) -> this would add 1 to the list making the list 1 spot longer than it just was

As far as I know, Common Lisp does not have a function for this, but it can easily be manipulated to do so:

(defparameter *hi* (make-array 3 :initial-contents '(1 2 3)))

(append (coerce *hi* 'list) '(4))



Output:

[28]>
(1 2 3 4)


See, I can very simply add an element to my array using the coerce function to first change it to a list and then appending the element I want to the end of it. However, note, to have the array be holding the 4th element after that code has been run we will need to use the setf function.

(setf *hi* (append (coerce *hi* 'list) '(4)))



[31]>
(1 2 3 4)

Now it's just a plain list again, so you could easily coerce it back into an array as we did above. This still might not be the best way to do it, but you could definitely use it and make whatever you wanted work as expected.

Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#5 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: adding values to arrays in Lisp

Posted 31 May 2011 - 06:58 PM

Just adding to my post above,

Here is a function that would do everything you wanted:

(defparameter *hi* (make-array 3 :initial-contents '(1 2 3)))

(defun add-to-array (n)
	(setf *hi* (append (coerce *hi* 'list) n))
	(setf *hi* (coerce *hi* 'array)))



Output:

[37]> (add-to-array '(1))

#(1 2 3 1)

You feed the function a list as a parameter and it adds it to the end of the array.
Was This Post Helpful? 2
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: adding values to arrays in Lisp

Posted 06 June 2011 - 09:16 PM

hey thanks!! i might end up using that :) im a bit late to respond but thanks any way :)
Was This Post Helpful? 0
  • +
  • -

#7 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: adding values to arrays in Lisp

Posted 07 June 2011 - 11:47 AM

You're welcome!
Was This Post Helpful? 0
  • +
  • -

#8 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: adding values to arrays in Lisp

Posted 09 June 2011 - 08:15 AM

Hi,


I just came across this, so definitely think I should clear it up. There actually IS a way to make an adjustable array without having to define your own function for it. As followed:

(defparameter *x* (make-array 3 :fill-pointer 0 :adjustable t)

; there are two key-words arguments in this. :fill-pointer and :adjustable.

; :fill-pointer - this allows you to add elements to your array as needed

> *x*

#()

; :adjustable - this allows you to expand the size of the array to add, in this case, 3 more spots

> (vector-push 'a *x*)

0

> (vector-push 'b *x*)

1

> (vector-push 'c *x*)

2

> (vector-push 'd *x*)

NIL

; see how this case failed? Well, if we now use vector-push-extend ...

> (vector-push-extend 'd *x*)

3

> *x*

#(A B C D)




:fill-pointer and :adjustable can be used independently, but if you do not use :fill-pointer you do not have access to (vector-push) or (vector-pop). vector-pop returns the most recently pushed element and decrements the fill-pointer in the process.

> (vector-pop *x*)

D

> *x*

#(A B C)





Just wanted to clear that all up!

This post has been edited by I X Code X 1: 09 June 2011 - 08:24 AM

Was This Post Helpful? 1
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: adding values to arrays in Lisp

Posted 09 June 2011 - 11:05 AM

ya i tried that when i first made the topic but for some reason or another my interpreter doesn't define those functions. oddly enough now it's working but i don't remember what i did differently between now and then. any way thanks :)

This post has been edited by ishkabible: 09 June 2011 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#10 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: adding values to arrays in Lisp

Posted 09 June 2011 - 11:32 AM

Happens to me a lot too. I try something, it doesn't work, try again later and for some odd reason it decides it wants to work this time :crazy:

This post has been edited by I X Code X 1: 09 June 2011 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#11 venum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-February 12

Re: adding values to arrays in Lisp

Posted 09 February 2012 - 01:27 AM

hi,
Is there any method by which i can add open or closed parenthesis as a character to a vector.

View PostI X Code X 1, on 09 June 2011 - 08:15 AM, said:

Hi,


I just came across this, so definitely think I should clear it up. There actually IS a way to make an adjustable array without having to define your own function for it. As followed:

(defparameter *x* (make-array 3 :fill-pointer 0 :adjustable t)

; there are two key-words arguments in this. :fill-pointer and :adjustable.

; :fill-pointer - this allows you to add elements to your array as needed

> *x*

#()

; :adjustable - this allows you to expand the size of the array to add, in this case, 3 more spots

> (vector-push 'a *x*)

0

> (vector-push 'b *x*)

1

> (vector-push 'c *x*)

2

> (vector-push 'd *x*)

NIL

; see how this case failed? Well, if we now use vector-push-extend ...

> (vector-push-extend 'd *x*)

3

> *x*

#(A B C D)




:fill-pointer and :adjustable can be used independently, but if you do not use :fill-pointer you do not have access to (vector-push) or (vector-pop). vector-pop returns the most recently pushed element and decrements the fill-pointer in the process.

> (vector-pop *x*)

D

> *x*

#(A B C)





Just wanted to clear that all up!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1