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.
adding values to arrays in Lisp
Page 1 of 110 Replies - 4194 Views - Last Post: 09 February 2012 - 01:27 AM
Replies To: adding values to arrays in Lisp
#2
Re: adding values to arrays in Lisp
Posted 18 April 2011 - 07:09 PM
Have you seen this:
Common Lisp Hints
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
#3
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.
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.
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.
#4
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:
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.
[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!
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!
#5
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:
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.
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.
#6
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
#8
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:
: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.
Just wanted to clear that all up!
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
#9
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
#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
This post has been edited by I X Code X 1: 09 June 2011 - 11:32 AM
#11
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.
Is there any method by which i can add open or closed parenthesis as a character to a vector.
I 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:
: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.
Just wanted to clear that all up!
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!
Page 1 of 1
|
|

New Topic/Question
Reply






MultiQuote



|