Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

 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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Bulat Ziganshin
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,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread 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,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

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,),(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,
  Bulatmailto:bulat.zigans...@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---tp26005244p26006471.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Ketil Malde
zaxis z_a...@163.com 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,),(3,3)] in
 function `f` ? 

Not sure if there's anything in Data.List, but you could do something
like: 

  f xs = (2,) : 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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

f xs = (2,) : 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 z_a...@163.com 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,),(3,3)] in
 function `f` ? 
 
 Not sure if there's anything in Data.List, but you could do something
 like: 
 
   f xs = (2,) : 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---tp26005244p26006947.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

f xs = (2,) : 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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

replace k v xs = (k,v) : filter ((/=v) . fst) xs
Great !  thanks you very much


minh thu wrote:
 
 2009/10/22 zaxis z_a...@163.com:

f xs = (2,) : 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---tp26005244p26007076.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Kyle Murphy
- 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
 let foo = [1,2,3]
don't think of foo as a variable that contains a list of numbers, instead
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 bulat.zigans...@gmail.comwrote:

 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,
  Bulatmailto:bulat.zigans...@gmail.com

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe