Home > Archive > Scheme > March 2006 > vector problems.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Player 2006-03-06, 3:57 am |
| Hello to all.
I am new to coding and am clunking my way through learnign scheme at my own
pace.
However I have come across a sticking point that is bugging me as I don't
want to let it slide and just carry on reading the texts I am using because
I know it will niggle at me in the back of my mind.
So to the problem...
If I create a vector and fill it like so..
(define v (make-vector 4))
(vector-set! v 0 123)
(vector-set! v 1 456)
(vector-set! v 2 (make-vector 5))
(vector-set! v 3 798)
I eventually, after much frustration and trial and error, figured out how to
populate an embedded vector within a vector via the following...
(vector-set! v 2 #5(1 2 3 4 5))
No thanks to the books I am using, [Teach yourself scheme in fix num days +
htdp] which give a very short paragraph each on vectors.....
Took me ages to figure out I had to use the #5(1 2 3 4 5) format, I ended up
figuring it out via the error msg's DrScheme kept spitting at me heh.
BUT, I still haven't figured out how I get a variable to represent, or
indead just extract a value from, one of the 2nd embedded vectors
slots/values/potitions.
I tried all these...
(vector-ref v 2 0) = error because it expects 2 args only.
(vector-ref v2 0) = undefined indentifier heh - tad obvious now i look
at it lol.
vector-ref v (1 0)) =procedure application: expected procedure, given: 1;
arguments were: 0
So as you can see I havent worked out how I can access say position 2 in the
embedded 2nd vector and/or bind its value to a variable, if i needed or
wnated to.
so at present vector v = #4(123 456 #5(1 2 3 4 5) 0)
AND, I want to access, and or bind to a variable, say potition 2 in embedded
vector of vector V at positition 2 - which would be 3.
It's no bigg deal or anything, it's just bugging me that I cant figure out
how it would be done.
Cheeers.
Player
| |
| Nils O. Selåsdal 2006-03-06, 3:57 am |
| Player wrote:
> Hello to all.
>
> I am new to coding and am clunking my way through learnign scheme at my own
> pace.
>
> However I have come across a sticking point that is bugging me as I don't
> want to let it slide and just carry on reading the texts I am using because
> I know it will niggle at me in the back of my mind.
>
> So to the problem...
>
> If I create a vector and fill it like so..
>
> (define v (make-vector 4))
> (vector-set! v 0 123)
> (vector-set! v 1 456)
> (vector-set! v 2 (make-vector 5))
> (vector-set! v 3 798)
>
> I eventually, after much frustration and trial and error, figured out how to
> populate an embedded vector within a vector via the following...
>
> (vector-set! v 2 #5(1 2 3 4 5))
>
> No thanks to the books I am using, [Teach yourself scheme in fix num days +
> htdp] which give a very short paragraph each on vectors.....
Perhaps http://srfi.schemers.org/srfi-43/srfi-43.html can help you.
| |
| Jens Axel Søgaard 2006-03-06, 3:57 am |
| Player wrote:
> BUT, I still haven't figured out how I get a variable to represent, or
> indead just extract a value from, one of the 2nd embedded vectors
> slots/values/potitions.
> (define m (vector
(vector 'a 'b 'c)
(vector 'd 'e 'f)
(vector 'g 'h 'i)))
> (let ([row (vector-ref m 0)])
(vector-ref row 2))
c
> (vector-ref (vector-ref m 0) 2)
c
--
Jens Axel Søgaard
| |
| Nate Thern 2006-03-09, 7:00 pm |
| Player-
Don't blame TYS, a tutorial and a reference are two different things
:-). Once you know enough from a tutorial to get yourself in trouble,
what you want is a reference. Since you're using DrScheme, you must have
access to the helpdesk. It will be invaluable to your scheme learning
experience. Since you're interested in vectors, search for "vector" in
the helpdesk. Unfortunately, the best stuff comes after a lot of hits
that you don't need right now. Scroll past the Extension Manual, Swindle
Manual and SRFI hits. Next comes the hits from "Revised^5 Report on the
Algorithmic Language Scheme" which everyone in this group calls "R5RS".
This should be your PRIMARY reference to modern scheme. The next
resource with hits is the mzscheme manual which should be a SECONDARY
reference to the implementation you are using. Without even entering
R5Rs we see that some vector functions are:
(vector obj ...) in "Vectors"
(vector? obj) in "Vectors"
(vector-set! vector k obj) in "Vectors"
(vector-ref vector k) in "Vectors"
(vector-length vector) in "Vectors"
(vector-fill! vector fill) in "Vectors"
(vector->list vector) in "Vectors"
(make-vector k fill) in "Vectors"
(make-vector k) in "Vectors"
(list->vector list) in "Vectors"
So you immediately re-write
>
> (define v (make-vector 4))
> (vector-set! v 0 123)
> (vector-set! v 1 456)
> (vector-set! v 2 (make-vector 5))
> (vector-set! v 3 798)
as
(define v (vector 123 456 (vector 1 2 3 4 5) 798))
Next you actually go to the "vectors" section of R5RS and find that
vectors can be input as #(...). So now you re-write again:
(define v #(123 456 #(1 2 3 4 5) 798))
You also learn that vectors contain heterogenous objects, analogous to
lists, so you can operate on the contents of a vector:
(vector-set! (vector-ref v 2) 2 13)
v => #(123 456 #(1 2 13 4 5) 798)
(+ (vector-ref v 3) 1) => 799
(+ (vector-ref (vector-ref v 2) 4) 1) => 6
Finally, you go to "vectors" in "Input Parsing" under the mzscheme
manual and learn that #5(1 2) is an mzscheme input extension which will
create #(1 2 2 2 2). So, of course, your
> (vector-set! v 2 #5(1 2 3 4 5))
is the same as (vector-set! v 2 #(1 2 3 4 5))
> So as you can see I havent worked out how I can access say position 2
in the
> embedded 2nd vector and/or bind its value to a variable, if i needed or
> wnated to.
>
> so at present vector v = #4(123 456 #5(1 2 3 4 5) 0)
>
> AND, I want to access, and or bind to a variable, say potition 2 in
embedded
> vector of vector V at positition 2 - which would be 3.
Now you can. R5RS is your friend. Get to know
(let ((author-pref 'him))
(cond ((traditionalist?) 'him) ((pc?) 'her) (else author-pref))).
Nate
| |
| Player 2006-03-09, 7:00 pm |
|
"Player" <why_do_you_want@to.know.com> wrote in message
news:dugodc$53l$1@news8.svr.pol.co.uk...
> Hello to all.
>
> I am new to coding and am clunking my way through learnign scheme at my
own
> pace.
>
> However I have come across a sticking point that is bugging me as I don't
> want to let it slide and just carry on reading the texts I am using
because
> I know it will niggle at me in the back of my mind.
>
> So to the problem...
>
> If I create a vector and fill it like so..
>
> (define v (make-vector 4))
> (vector-set! v 0 123)
> (vector-set! v 1 456)
> (vector-set! v 2 (make-vector 5))
> (vector-set! v 3 798)
>
> I eventually, after much frustration and trial and error, figured out how
to
> populate an embedded vector within a vector via the following...
>
> (vector-set! v 2 #5(1 2 3 4 5))
>
> No thanks to the books I am using, [Teach yourself scheme in fix num days
+
> htdp] which give a very short paragraph each on vectors.....
>
> Took me ages to figure out I had to use the #5(1 2 3 4 5) format, I ended
up
> figuring it out via the error msg's DrScheme kept spitting at me heh.
>
> BUT, I still haven't figured out how I get a variable to represent, or
> indead just extract a value from, one of the 2nd embedded vectors
> slots/values/potitions.
>
> I tried all these...
>
> (vector-ref v 2 0) = error because it expects 2 args only.
> (vector-ref v2 0) = undefined indentifier heh - tad obvious now i
look
> at it lol.
> vector-ref v (1 0)) =procedure application: expected procedure, given:
1;
> arguments were: 0
>
> So as you can see I havent worked out how I can access say position 2 in
the
> embedded 2nd vector and/or bind its value to a variable, if i needed or
> wnated to.
>
> so at present vector v = #4(123 456 #5(1 2 3 4 5) 0)
>
> AND, I want to access, and or bind to a variable, say potition 2 in
embedded
> vector of vector V at positition 2 - which would be 3.
>
> It's no bigg deal or anything, it's just bugging me that I cant figure out
> how it would be done.
>
> Cheeers.
> Player
>
Cheers guy for the replies, they're very helpfull :)
Hyperion
| |
| Ray Dillinger 2006-03-09, 7:00 pm |
| Nate Thern wrote:
> So you immediately re-write
> as
> (define v (vector 123 456 (vector 1 2 3 4 5) 798))
This is good advice.
> Next you actually go to the "vectors" section of R5RS and find that
> vectors can be input as #(...). So now you re-write again:
> (define v #(123 456 #(1 2 3 4 5) 798))
This is questionable advice. As far as I can see there is
no guarantee that vectors written literally in external form
in the code are mutable. IE, with the above definition,
(vector-set! v ...) "is an error" which different
implementations are free to interpret in various ways.
While most will do it, there is nothing in the standard
that commands them to.
bear
|
|
|
|
|