Change the entries in an array in Haskell
up vote
0
down vote
favorite
Suppose you have
let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]
Now I want that to multiply the 3 in the last tuple with some number. How can I do that?
arrays haskell
add a comment |
up vote
0
down vote
favorite
Suppose you have
let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]
Now I want that to multiply the 3 in the last tuple with some number. How can I do that?
arrays haskell
2
what did you try?
– OznOg
Nov 10 at 20:22
Whicharray
function is this?
– melpomene
Nov 10 at 20:25
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Suppose you have
let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]
Now I want that to multiply the 3 in the last tuple with some number. How can I do that?
arrays haskell
Suppose you have
let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]
Now I want that to multiply the 3 in the last tuple with some number. How can I do that?
arrays haskell
arrays haskell
asked Nov 10 at 20:18
Dreikäsehoch
1135
1135
2
what did you try?
– OznOg
Nov 10 at 20:22
Whicharray
function is this?
– melpomene
Nov 10 at 20:25
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37
add a comment |
2
what did you try?
– OznOg
Nov 10 at 20:22
Whicharray
function is this?
– melpomene
Nov 10 at 20:25
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37
2
2
what did you try?
– OznOg
Nov 10 at 20:22
what did you try?
– OznOg
Nov 10 at 20:22
Which
array
function is this?– melpomene
Nov 10 at 20:25
Which
array
function is this?– melpomene
Nov 10 at 20:25
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
If you wanted to multiply that by 5
it would be:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
The signature of this function reads
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
and its documentation says:
accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.
So calling accum f arrayValue listWithPairsOfIndicesAndValues
will call f
for every index given in listWithPairsOfIndicesAndValues
providing the old value and the value from listWithPairsOfIndicesAndValues
for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues
updated with the values returned by the respective calls to f
.
add a comment |
up vote
4
down vote
There's two different ways to your goal:
Using the incremental update:
a // [((2,2),(3*7))]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))
Using the accumulation:
accum (*) a [((2,2), 7)]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
add a comment |
up vote
2
down vote
One could use a // [(2,2), n * (a ! (2,2))]
to multiply the element with index (2,2)
by n
.
(There should also be some lens-y approach with a better syntax)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
If you wanted to multiply that by 5
it would be:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
The signature of this function reads
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
and its documentation says:
accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.
So calling accum f arrayValue listWithPairsOfIndicesAndValues
will call f
for every index given in listWithPairsOfIndicesAndValues
providing the old value and the value from listWithPairsOfIndicesAndValues
for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues
updated with the values returned by the respective calls to f
.
add a comment |
up vote
5
down vote
accepted
If you wanted to multiply that by 5
it would be:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
The signature of this function reads
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
and its documentation says:
accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.
So calling accum f arrayValue listWithPairsOfIndicesAndValues
will call f
for every index given in listWithPairsOfIndicesAndValues
providing the old value and the value from listWithPairsOfIndicesAndValues
for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues
updated with the values returned by the respective calls to f
.
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
If you wanted to multiply that by 5
it would be:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
The signature of this function reads
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
and its documentation says:
accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.
So calling accum f arrayValue listWithPairsOfIndicesAndValues
will call f
for every index given in listWithPairsOfIndicesAndValues
providing the old value and the value from listWithPairsOfIndicesAndValues
for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues
updated with the values returned by the respective calls to f
.
If you wanted to multiply that by 5
it would be:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
The signature of this function reads
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
and its documentation says:
accum f takes an array and an association list and accumulates pairs
from the list into the array with the accumulating function f.
So calling accum f arrayValue listWithPairsOfIndicesAndValues
will call f
for every index given in listWithPairsOfIndicesAndValues
providing the old value and the value from listWithPairsOfIndicesAndValues
for this index and return a new array with all the positions mentioned in listWithPairsOfIndicesAndValues
updated with the values returned by the respective calls to f
.
edited Nov 10 at 20:52
answered Nov 10 at 20:46
typetetris
2,615323
2,615323
add a comment |
add a comment |
up vote
4
down vote
There's two different ways to your goal:
Using the incremental update:
a // [((2,2),(3*7))]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))
Using the accumulation:
accum (*) a [((2,2), 7)]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
add a comment |
up vote
4
down vote
There's two different ways to your goal:
Using the incremental update:
a // [((2,2),(3*7))]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))
Using the accumulation:
accum (*) a [((2,2), 7)]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
add a comment |
up vote
4
down vote
up vote
4
down vote
There's two different ways to your goal:
Using the incremental update:
a // [((2,2),(3*7))]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))
Using the accumulation:
accum (*) a [((2,2), 7)]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
There's two different ways to your goal:
Using the incremental update:
a // [((2,2),(3*7))]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))
Using the accumulation:
accum (*) a [((2,2), 7)]
>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]
answered Nov 10 at 20:51
RoyM
746
746
add a comment |
add a comment |
up vote
2
down vote
One could use a // [(2,2), n * (a ! (2,2))]
to multiply the element with index (2,2)
by n
.
(There should also be some lens-y approach with a better syntax)
add a comment |
up vote
2
down vote
One could use a // [(2,2), n * (a ! (2,2))]
to multiply the element with index (2,2)
by n
.
(There should also be some lens-y approach with a better syntax)
add a comment |
up vote
2
down vote
up vote
2
down vote
One could use a // [(2,2), n * (a ! (2,2))]
to multiply the element with index (2,2)
by n
.
(There should also be some lens-y approach with a better syntax)
One could use a // [(2,2), n * (a ! (2,2))]
to multiply the element with index (2,2)
by n
.
(There should also be some lens-y approach with a better syntax)
answered Nov 10 at 20:48
chi
71.8k279132
71.8k279132
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243052%2fchange-the-entries-in-an-array-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
what did you try?
– OznOg
Nov 10 at 20:22
Which
array
function is this?– melpomene
Nov 10 at 20:25
I tried to make an accumArray, but that didn't help much. I also tried with product(snd (unzip (assocs a))) but then I don't know how to put the result in the array.
– Dreikäsehoch
Nov 10 at 20:27
@melpomene the array from Data.Array, i guess
– Dreikäsehoch
Nov 10 at 20:30
If you want to modify entries, why are you using an array?
– melpomene
Nov 10 at 20:37