[Haskell-beginners] Can't cast an IO String to s Astring

Rein Henrichs rein.henrichs at gmail.com
Fri Oct 23 17:16:09 UTC 2015


You can't transform an IO String into a String. This is one of the purposes
of monads: to make it possible to work with things like IO, where it is
impossible to turn an IO a into an a. The result of performing an IO action
can be *bound* using (>>=) or <- in do notation so that the String is
available for the rest of the computation:

do
  f <- readFile "ctrl.txt"
  Right r <- parseCtrl f
  cherchectrl r "A01"

Note that there's no need to wrap identifiers in (): (r) is the same as r,
(rez) is the same as rez. And there's no need to say do { let r =
something; return r }. You can just say do { something }.

On Fri, Oct 23, 2015 at 8:32 AM chanti houda <houdachanti at yahoo.fr> wrote:

> Hello, I'm writing a Haskell code which consists to read a text file,
> parse it and thansform the parsing result on a specific language.
> I have a function *affiche *which takes a data type Ctrl and returns a
> String. This is the transformation funtion.
> I have also anothe function parsctrl, which parse the contents of a text
> file ("ctrl.txt") and after looks for a specific value ("A01") in the parse
> result (function *c**herchectrl*).
> I need to use the result of the *parsectrl *function in another function
> *fc*.
> The code is composed of three functions
>
>
>
> parsectrl = do
>  f <- readFile "ctrl.txt"
>  let Right r = parse  parseCtrl  " " f
>  let rez =cherchectrl ( r) "A01"
>  return (rez)
>
> fc[]  =[]
> fc((door,[(all,v1),(alt,v2),(lint,v3),(w,v4),(r,v5),(loc,v6),(
> etat,v7),(ruin,v8)]):ls ) = ("&OUV ID='"++door ++"', ALLEGE="++show((moi
> v1)/1000)++", LINTEAU="++show((moi v3)/1000)++", LARGEUR="++show((moi
> v4)/1000)++", COEF=0.7, ALT="++show((moi v2)/1000)++", LOCIDS='"++v6++"',
> CTRLID='"++ v7++"', CTRLID_RUIN='"++ v8++" /" ++"\n" ++"&CTRL
> ID='"++v7++"', " ++ "ON_INI=.FALSE., DURATION=0 / \n"++"&CTRL
> ID='"++v8++"', LOCID='"++  ((parses("'"++v6++"'"))!!0) ++
> *affiche(parsectrl)*++ " / \n\n"  )++fc(ls)
>
> The parsectrl returns an IO String, but the function affiche needs a
> String as input and even when I tried to adapt the affiche function to take
> an  IO String -> String I can't.
> the result of fc must be a String too.
>
> The IO String comes from the parsectrl function.
> Can you help me to solve this problem: how can I transform an IO String to
> a String.
>
> Thank you by advance.
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151023/eacf0a43/attachment.html>


More information about the Beginners mailing list