How can i safely change the value of specified key ?

aaa <- newIORef ([]::[(Int,Int)]) writeIORef aaa [(1,1),(2,2),(3,3)] then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way ? re-write aaa is not permitted. Sincerely! -- View this message in context: http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---t... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2009/10/22 zaxis
aaa <- newIORef ([]::[(Int,Int)]) writeIORef aaa [(1,1),(2,2),(3,3)]
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way ? re-write aaa is not permitted.
Why do you say that ? You can use again writeIORef of modifyIORef to change aaa's content. Cheers, Thu

Hello zaxis, Thursday, October 22, 2009, 11:28:14 AM, you wrote:
aaa <- newIORef ([]::[(Int,Int)]) writeIORef aaa [(1,1),(2,2),(3,3)]
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way ? re-write aaa is not permitted.
it's the only way. in Haskell, you have *immutable* values. aaa is a reference to immutable value. you can mutate reference so it will point to another immutable value but you cannot change this value. there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can apply function to whole value: value <- readIORef aaa writeIORef aaa (f value) second, you may create list of IORefs, tuple of IORefs and so: a <- newIORef (1,1) ... let aaa = [a,b,c] now aaa is a immutable list of mutable IORefs. of course, you can create IORef pointing to list of IORefs too -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

value <- readIORef aaa writeIORef aaa (f value)
then aaa will *point to* a new value. The original value will be Garbage Collected, right ? BTW, Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best way to change it to [(1,1),(2,2222),(3,3)] in function `f` ? Bulat Ziganshin-2 wrote:
Hello zaxis,
Thursday, October 22, 2009, 11:28:14 AM, you wrote:
aaa <- newIORef ([]::[(Int,Int)]) writeIORef aaa [(1,1),(2,2),(3,3)]
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way ? re-write aaa is not permitted.
it's the only way. in Haskell, you have *immutable* values. aaa is a reference to immutable value. you can mutate reference so it will point to another immutable value but you cannot change this value.
there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can apply function to whole value:
value <- readIORef aaa writeIORef aaa (f value)
second, you may create list of IORefs, tuple of IORefs and so:
a <- newIORef (1,1) ... let aaa = [a,b,c]
now aaa is a immutable list of mutable IORefs. of course, you can create IORef pointing to list of IORefs too
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---t... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

zaxis
value <- readIORef aaa writeIORef aaa (f value)
then aaa will *point to* a new value.
Exactly. That's what IORefs are, references pointing to contents that can be changed in the IO monad.
The original value will be Garbage Collected, right ?
Yes, unless something else is holding on to it.
Is [(1,1),(2,2),(3,3)] been regarded as a hash?
It's a list of pairs. You can treat it as an association list using e.g. 'lookup' from Data.List, if that's what you mean.
If not, what is the best way to change it to [(1,1),(2,2222),(3,3)] in function `f` ?
Not sure if there's anything in Data.List, but you could do something like: f xs = (2,2222) : filter ((/=2) . fst) xs -k -- If I haven't seen further, it is by standing in the footprints of giants

f xs = (2,2222) : filter ((/=2) . fst) xs It works but not general as `f` still needs to change other value according to the "KEY". Maybe Data.List will supply what i need.
Ketil Malde-5 wrote:
zaxis
writes: value <- readIORef aaa writeIORef aaa (f value)
then aaa will *point to* a new value.
Exactly. That's what IORefs are, references pointing to contents that can be changed in the IO monad.
The original value will be Garbage Collected, right ?
Yes, unless something else is holding on to it.
Is [(1,1),(2,2),(3,3)] been regarded as a hash?
It's a list of pairs. You can treat it as an association list using e.g. 'lookup' from Data.List, if that's what you mean.
If not, what is the best way to change it to [(1,1),(2,2222),(3,3)] in function `f` ?
Not sure if there's anything in Data.List, but you could do something like:
f xs = (2,2222) : filter ((/=2) . fst) xs
-k -- If I haven't seen further, it is by standing in the footprints of giants _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---t... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2009/10/22 zaxis
f xs = (2,2222) : filter ((/=2) . fst) xs It works but not general as `f` still needs to change other value according to the "KEY". Maybe Data.List will supply what i need.
I don't think Data.List has what you want as-is. The above code is generalized simply: replace k v xs = (k,v) : filter ((/=v) . fst) xs Cheers, Thu

replace k v xs = (k,v) : filter ((/=v) . fst) xs Great ! thanks you very much
minh thu wrote:
2009/10/22 zaxis
: f xs = (2,2222) : filter ((/=2) . fst) xs It works but not general as `f` still needs to change other value according to the "KEY". Maybe Data.List will supply what i need.
I don't think Data.List has what you want as-is. The above code is generalized simply: replace k v xs = (k,v) : filter ((/=v) . fst) xs
Cheers, Thu _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---t... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2009/10/22 zaxis
replace k v xs = (k,v) : filter ((/=v) . fst) xs Great ! thanks you very much
I'm not sure why you're so much happy: Assume some function defined as follow: foo a b c = e x y z a b c where x = some constant y = some other constant z = some other constant 'e' means just that the complete body of the function foo involves x, y, z, a, b and c. Generalizing foo to let the caller choose the values of x, y, z is just foo a b c x y z = e x y z a b c -- the where is unneeded In fact, this is what functions are made for: generalizing a given expression so that part of the expression can be given in arguements. E.g. turning e a b into f a = e a b Cheers, Thu

Hello zaxis, Thursday, October 22, 2009, 1:03:21 PM, you wrote:
value <- readIORef aaa writeIORef aaa (f value) then aaa will *point to* a new value. The original value will be Garbage Collected, right ?
yes, exactly
BTW, Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best way to change it to [(1,1),(2,2222),(3,3)] in function `f` ?
hm, i'm frequently use lists of tuples as dictionaries, there is Data.List.lookup function that perfroms lookup. but these are stored naively so it's useful only for small dictionaries. if you need hash, you can use Data.Hashtable module (it provides hash with imperative access), or use Data.Map that has pure interface. again, for the first time i strongly recommend to learn Data.Map. if you will learn Haskell as imperative language, you will find it slow, awkward and useless. if you will learn functional features and only then go to imperative part, you may find it much more pleasant just one example from my code: repeat_while (receiveP pipe) notTheEnd $ \(FileStart fi) -> do extract_file command fi $ \writer -> do repeat_while (receiveP pipe) notDataEnd (\(DataChunk buf len) -> do writer buf len; send_backP pipe (buf,len)) yes, it's imperative but it includes several lambdas per each line
Bulat Ziganshin-2 wrote:
Hello zaxis,
Thursday, October 22, 2009, 11:28:14 AM, you wrote:
aaa <- newIORef ([]::[(Int,Int)]) writeIORef aaa [(1,1),(2,2),(3,3)]
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way ? re-write aaa is not permitted.
it's the only way. in Haskell, you have *immutable* values. aaa is a reference to immutable value. you can mutate reference so it will point to another immutable value but you cannot change this value.
there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can apply function to whole value:
value <- readIORef aaa writeIORef aaa (f value)
second, you may create list of IORefs, tuple of IORefs and so:
a <- newIORef (1,1) ... let aaa = [a,b,c]
now aaa is a immutable list of mutable IORefs. of course, you can create IORef pointing to list of IORefs too
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hello zaxis, Thursday, October 22, 2009, 1:03:21 PM, you wrote:
Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best way to change it to [(1,1),(2,2222),(3,3)] in function `f` ?
f = map (\x@(a,b) -> if a==2 then (a,2222) else x) or f xs = [(a, if a==2 then 2222 else b) | (a,b) <- xs] but anyway, modifying one value inside complex datastructure is not FP way. you should consider writing a function that maps your entire input data to output one -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hello zaxis, Thursday, October 22, 2009, 11:28:14 AM, you wrote:
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
... well, anyway what you are doing isn't very haskellish. it may be considered as advanced topic but basically, best way to compute something in Haskell is to construct pure function -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

let foo = [1,2,3] don't think of foo as a variable that contains a list of numbers, instead
- Sorry for the late reply on this, I actually sent it last night before I went to bed but accidentally sent it only to Bulat, re-sending it now for the entire list - As Bulat said, it sounds like you're not fully understanding functional programming. You really should read one of the better tutorials or books on Haskell like Real World Haskell (you can read it for free online, just google for it), as it seems like you're trying to use a advanced feature of the language (IORefs) that's designed to help solve some particularly difficult problems in order to treat the language more like a procedural language (which is a really bad way to approach it). This would be something like if you decided to learn Java by only ever have one class that consisted of nothing but a whole bunch of public static methods, it pretty much misses the point of the language. Try not to think of variables as places to store values, instead think of them as functions that take zero or more arguments. when you do something like think of foo as a function that doesn't take any arguments and returns a list of numbers (in this case a constant list). You should also concentrate on ways data can be transformed particularly using higher order functions like map and foldr. For instance, if you want to convert a list like [1,2,3] into one like [(1,1),(2,2),(3,3)] (that is, convert [Int] to [(Int, Int)]) you can use map or zip like so:
map (\x -> (x,x)) [1,2,3] -- applies a function to each member, in this case the lambda \x -> (x,x) which just makes a tuple zip [1,2,3] [1,2,3] -- combines two lists into one by making each element of the joined list a tuple of one element from each list
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Thu, Oct 22, 2009 at 03:41, Bulat Ziganshin
Hello zaxis,
Thursday, October 22, 2009, 11:28:14 AM, you wrote:
then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
... well, anyway what you are doing isn't very haskellish. it may be considered as advanced topic but basically, best way to compute something in Haskell is to construct pure function
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Bulat Ziganshin
-
Ketil Malde
-
Kyle Murphy
-
minh thu
-
zaxis