[Haskell-beginners] 'Simple' function

Mike Houghton mike_k_houghton at yahoo.co.uk
Wed Jun 10 17:50:04 UTC 2015


Thanks for all the replies! 
It’s become a little clearer. However…
(again this is naive begginer stuff.. )
if the signature is

asString :: IO String -> String

why is this not a pure function? The IO string has already been  supplied - maybe via keyboard input - and so
for the same IO String the function will always return the  same value. Surely this behaviour is different to a monadic function that
reads the keyboard and its  output (rather than the input) could be different. 
ie if I give asString an input of   IO “myString” then it will always return “myString” every time I invoke it with IO “myString”

Many thanks

Mike




> On 10 Jun 2015, at 18:20, Imants Cekusins <imantc at gmail.com> wrote:
> 
> Mike, if you are trying to run a "hello world" program in ghci, here
> are 2 working functions.
> 
> -- #1 : all it does is prompts for input and sends the value back to IO
> 
> module Text where
> 
> ioStr :: IO()
> ioStr = do
>  putStrLn "enter anything"
>  str <- getLine
>  putStrLn str
> 
> 
> -- #2 this program prepends the string you pass to it as an arg with "Hello"
> 
> str2str:: String -> String
> str2str s = "Hello " ++ s
> 
> 
> -- how to run:
> -- #1 : ioStr
> -- #2 : str2str "some text"
> 
> hope this helps
> 
> 
> On 10 June 2015 at 19:08, aldiyen <aldiyen at aldiyen.com> wrote:
>> And just as a note, you can't really ever get the value inside the IO monad out. IO is not pure / non-deterministic, since it depends on something outside the program, and there's no way to "make it pure", as it were. You have to do all your operations on that String within the context of an IO
>> 
>> -aldiyen
>> 
>> 
>> 
>>> On Jun 10, 2015, at 12:47, Steven Williams <theblessedadventhope at gmail.com> wrote:
>>> 
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>> 
>>> Here is return's type signature:
>>> 
>>> return :: Monad m => a -> m a
>>> 
>>> What you are doing with the do notation can also be expressed as ioStr
>>>>> = (\str -> return str).
>>> 
>>> do notation and bind both require you to have a value that has the
>>> same monad as before.
>>> 
>>> Steven Williams
>>> My PGP Key: http://pgp.mit.edu/pks/lookup?op=get&search=0xCACA6C74669A54
>>> FA
>>> 
>>>> On 10/06/15 12:35, Mike Houghton wrote:
>>>> Hi,
>>>> 
>>>> I’ve been tryimg to write a function  with signature
>>>> 
>>>> asString :: IO String -> String
>>>> 
>>>> 
>>>> Does someone please have the patience to explain to me what the
>>>> compiler error messages really mean for these two attempts and
>>>> exactly what I’m doing (!!!) If I *do not* give this function any
>>>> type signature then it works i.e..
>>>> 
>>>> asString ioStr = do str <- ioStr return $ str
>>>> 
>>>> and the compiler tells me its signature is
>>>> 
>>>> asString :: forall (m :: * -> *) b. Monad m => m b -> m b
>>>> 
>>>> which, at this stage of my Haskell progress, is just pure Voodoo.
>>>> Why isn’t it’s signature  asString :: IO String -> String ?
>>>> 
>>>> 
>>>> Another naive attempt is asString ioStr = str where str <- ioStr
>>>> 
>>>> and then compiler says parse error on input ‘<-’
>>>> 
>>>> 
>>>> Many Thanks
>>>> 
>>>> Mike
>>>> 
>>>> _______________________________________________ Beginners mailing
>>>> list Beginners at haskell.org
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>> -----BEGIN PGP SIGNATURE-----
>>> Version: GnuPG v2
>>> 
>>> iQIcBAEBAgAGBQJVeGo5AAoJEMrKbHRmmlT6N8UP/i/tAhDtyHiG3sgH3e5xAqyt
>>> JAsyX2JaBQVjERRVaJQy1+Pg9hNdGBCrVljxY0BH5B8np956bnuIEyZKtSc2i2Jc
>>> HM0lBesyzCYqw29QxAyFFno07iXQllocZaHUIgC4AoNYO5zNGSPYcNaB4O5SYoKl
>>> 83Cjz97BHgAHkvHpsLDLOpizOkP+CsXwi8s/KRKoidLkbQpmv9SpqiFvmm9u+UK1
>>> emZF/4veFE4Ay3AvIsxMpn7M5hVoKgat1xyGX02IrenvkOL69IIYc+4OvzK49Lxg
>>> e8jrAehJDMh+U7zN+qVCY1ZyJbJF+uGawFC+XoswOdAra+Q23te77RKkligkmN7s
>>> ACut72hwTejZN/sIaORqZXuy+HUY1LjlJnlz0RCdG1CLkr3EaKG5ZCX3E2N8RnxL
>>> 1CKtEdtFJGDeBcIBh5my/7IC22loTpVhBhPU2DPo+iOP2sRsUs0nllbqbjGfGpuE
>>> m37dR/tfq9FKwqYS5RUuAcZ8fWuPdojmO2WvI4thHBGJhsRK4gqhAI4MnKLHBEoL
>>> xfyHSaoFif/jC7peF/+ZPjKSsIpCJU+R/tDUBM9u22o3IVeTs1sWGZXM7J32tlGc
>>> K/MTF/F3phcxwSCqb99WBHhXOIkKSgp47gx1INgDZFug/CgjUI1Sl4jvZ5j/45D5
>>> +RlHcYv+qp4J8nI59pFW
>>> =Vunc
>>> -----END PGP SIGNATURE-----
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



More information about the Beginners mailing list