[Haskell-beginners] 'Simple' function

Yannis Juglaret yjuglaret at gmail.com
Wed Jun 10 18:59:54 UTC 2015


To be complete, my message actually assumes a function of that type
*with the behavior you want*, which would be that of unsafePerformIO. Of
course a trivial *pure* function with that type is for instance:

  asString _ = "Hi"

But it does not have the behavior you want, it just ignores its argument.

-- Yannis

On 10/06/2015 20:22, Yannis Juglaret wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Assuming
> 
>   asString :: IO String -> String
> 
> we have
> 
>   getLine :: IO String
> 
>   asString getLine :: String
> 
> Yet
> 
>   asString getLine
> 
> could be "Hello" the first time you use it, then "Hi" the second time
> you use it. Same argument, different result, so this is not a pure
> function.
> 
> - -- Yannis
> 
> On 10/06/2015 19:50, Mike Houghton wrote:
>> 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:
>>>>>
>> 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
>>>>>
>>>>>>>
> _______________________________________________
>>>>> 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
>>
>> _______________________________________________ Beginners mailing
>> list Beginners at haskell.org 
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> 
> - -- 
> Yannis JUGLARET
> 

-- 
Yannis JUGLARET


More information about the Beginners mailing list