[Haskell-cafe] unsafeDestructiveAssign?
Daniel Peebles
pumpkingod at gmail.com
Tue Aug 11 12:08:08 EDT 2009
In both of your examples, a simple let would allow you to rebind the
name (not the same as assigning, but it didn't look like you need that
in those examples at least).
Note that you're already in IO, so you can use an IORef if you want a
true mutable variable, or an STRef in another function if you want to
maintain a pure interface.
But I'd strongly encourage you to look for alternate options here.
Just because it's possible to use mutable variables in Haskell doesn't
mean it's always a good idea to do so. Especially if you're new to the
language, I'd suggest doing your best to avoid approaches from other
languages.
But in general, if something "looks" pure (like your "variables" in
your email), you may be able to secretly mutate it behind the scenes,
but the compiler may not even notice. One advantage of purity is that
the compiler knows for sure that something won't change if you read it
multiple times. If you go changing its value (assuming you find some
hack that even allows it), you'll break many fundamental assumptions.
Hope this helps!
Dan
On Tue, Aug 11, 2009 at 11:48 AM, Job Vranish<jvranish at gmail.com> wrote:
> Does anybody know if there is some unsafe IO function that would let me do
> destructive assignment?
> Something like:
>
> a = 5
> main = do
> veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8
> print a
>> 8
>
> and yes I am in fact insane...
>
> I'm also looking for a way to make actual copies of data.
> so I could do something like this:
>
> a = Node 5 [Node 2 [], Node 5 [a]]
> main = do
> b <- makeCopy a
> veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign b (Node
> 0 [])
> -- 'a' is unchanged
>
> It would be even more fantastic, if the copy function was lazy.
> I think the traverse function might actually make a copy, but I would be
> happier with something more general (doesn't require membership in
> traversable), and more explicit (was actually designed for making real
> copies).
>
> Any ideas?
>
> Thanks,
>
> - Job
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
More information about the Haskell-Cafe
mailing list