[Haskell-cafe] Converting IO [XmlTree] to [XmlTree]
jules at jellybean.co.uk
Tue Apr 14 11:54:48 EDT 2009
Cristiano Paris wrote:
> On Tue, Apr 14, 2009 at 5:09 PM, Luke Palmer <lrpalmer at gmail.com> wrote:
>> Please don't say that. He's a beginner.
>> You realize that the path of least resistance will be to use it, right?
>> You see why that's not a good thing?
>> Even experts don't use this function.
>> (To the O.P.: don't use it)
> Mmmh, sorry Luke but I don't understand this ostracism.
> unsafePerformIO is not "evil" by itself, it's there for a purpose and,
> as for anything else in the language, it's better to understand when
> to use it and when not rather than just knowing that is something that
> MUST not be used, without any further explanation.
Sure, the explanation is there if people are interested in it.
However, in context, your answer was wrong. It is like someone asking:
"How do I get hold of a new phone"
and the answer
"Pull a gun on someone walking down the street and demand they give you
...that is, the answer was solving the wrong problem, or solving it in
the wrong context.
If you have IO [XmlTree], then you don't have an [XmlTree] at all -
rather you have a description of an (IO-involving) action which you need
to run to get one. You can run it many times, or once, or never. It will
(in general) give different results depending exactly when you run it.
Therefore you need to carefully decide when to run it - i.e. attach it
indirectly or directly into your main action, as the various other
answers have shown.
unsafePerformIO is not part of the haskell language - it does not
respect the type system. It is an extension mechanism which allows us to
add hooks into the RTS; effectively a way to extend the language. This
is a useful and powerful thing, but nothing in the questioner's question
suggested that language extension was what they wanted.
More information about the Haskell-Cafe