[Haskell-beginners] the role of assignments

Benjamin Edwards edwards.benj at gmail.com
Thu Jul 1 20:12:23 EDT 2010


On 2 July 2010 01:59, prad <prad at towardsfreedom.com> wrote:

> i'm trying to sort the assignment concept out in my mind.
> as i understand it so far,
>
> <- is for IO
>

Not really :)

<- is for working with monads. It is just syntactic sugar that makes using
monads a bit more pleasant


> so you can do something like
> someIOVar <- readFile filename
> this will be of type IO String

which is different from String as in
>

Yes, because underneath what is really going on is the compiler is changing
that to

readFile fileName >>= \x -> ...

let someStrVar = "this is a string"
>
> to work with an IO String you need to convert it into a String which
> seems to automatically happen inside a do structure as in:
>
> main = do
>    tmplX <- readFile "zztmpl.htm"
>    navbx <- readFile "zznavb.htm"
>    let page = U.replace tmplX "NAVX" navbx
>
> are there some other ways to make IO String into String?
>
>
Not that you should be using before leaning how monads work ;) But there is
unsafePerformIO


> also, it seems that assignment is different for the '=' in a program vs
> ghci for functions:
>
> sum x y = x + y (program)
> vs
> let sum x y = x + y (ghci)
>
>
in ghci you are *inside* the IO monad. Hence  the need for let bindings


> but not for strings and other things because you must always preface
> assignment of values with 'let':
>
> let a = 4
>
> i suppose the let is there for variable assignments because such things
> necessitate a change of state and i've read that this is not desirable
> in functional programming - you have to work a little harder to do
> assignment than languages which just allow
> a = 4
> b = 5
> c = 6
> etc
>
> in haskell, is it preferable to do something like this:
>
> var <- readFile fn
> someFunction var
>
> or someFunction (readFile fn)
>
>
actually this won't work, unless somefunction has type IO String -> IO a.
You would need

somefunction =<< readFile fn

and that would have type IO a assuming somefunction returns something of
type a


> --
> In friendship,
> prad
>
>                                      ... with you on your journey
> Towards Freedom
> http://www.towardsfreedom.com (website)
> Information, Inspiration, Imagination - truly a site for soaring I's
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>

You definitely need to read up on monads and the do notation. All will
become clearer :)

Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100701/9c5b092c/attachment.html


More information about the Beginners mailing list