Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: 'Simple' function (Yannis Juglaret)
2. Re: 'Simple' function (Marcin Mrotek)
3. Escaping special characters in text (Thomas Koster)
4. Re: Escaping special characters in text (Stefan H?ck)
5. Packages (Mike Houghton)
6. Re: Packages (Sumit Sahrawat, Maths & Computing, IIT (BHU))
7. Re: Packages (Mike Houghton)
----------------------------------------------------------------------
Message: 1
Date: Wed, 10 Jun 2015 20:59:54 +0200
From: Yannis Juglaret <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
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 <[email protected]>
>>> 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 <[email protected]> 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
>>>>> <[email protected]> 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 [email protected]
>>>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>
>>>>>>>
> _______________________________________________
>>>>> Beginners mailing list [email protected]
>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>> _______________________________________________ Beginners
>>>> mailing list [email protected]
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>> _______________________________________________ Beginners mailing
>>> list [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>> _______________________________________________ Beginners mailing
>> list [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
> - --
> Yannis JUGLARET
>
--
Yannis JUGLARET
------------------------------
Message: 2
Date: Wed, 10 Jun 2015 21:00:17 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID:
<CAJcfPzkaEDUyRY6RgYZ7atuwPc7L2W4G4776TF_=qj0fhhg...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
> This is a common source of confusion. A value of type IO a for some a is not
> an impure function because it is not a function. Its evaluation is
> completely pure and referentially transparent: every time you evaluate
> `getLine`, you get the same IO String value. The only observable difference
> is under execution, but we don't expect execution to be pure: we only expect
> evaluation to be pure.
Yeah, this is what I was trying to say in the latter part of my post,
but I guess I ended up confusing the matter more :(
Best regards,
Marcin Mrotek
------------------------------
Message: 3
Date: Thu, 11 Jun 2015 15:53:41 +1000
From: Thomas Koster <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Escaping special characters in text
Message-ID:
<CAG1wH7CnNiaK=XiLSt128uXpvU=lwm9ijpjvfpap9ayes6_...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Hi list,
My program needs to escape and unescape "special characters" in text
(Data.Text.Text), using my own definition of "special character"
(isSpecial :: Char -> Bool). I am looking for a library that provides
functions that implement or help me implement this functionality. I
don't really care exactly how the special characters are escaped, but
my preference is to prefix them with backslashes.
While "attoparsec" does technically answer my question, it is as
unimpressive an answer as "Prelude" unless the answer comes with a
particularly clever and concise parser that blows my mind (and then
kudos to the author). I am looking for a higher level library where I
don't need to re-invent this wheel. That is, I don't want to write an
unescaping parser if somebody has already published one on Hackage in
a clean, well-tested library.
My searches on Hoogle have turned up only network-uri, which offers
percent-encoding with the definition of "special character" accepted
as an argument [1]. This is the sort of thing I am after, although to
use network-uri I would have to round-trip via String, something that
I feel I should avoid. Functions of text types that return lazy text
builders would be ideal. Also, percent-encoding is not my favourite
encoding scheme.
Thanks in advance.
[1]
https://hackage.haskell.org/package/network-uri/docs/Network-URI.html#v:escapeURIString
--
Thomas Koster
------------------------------
Message: 4
Date: Thu, 11 Jun 2015 08:55:15 +0200
From: Stefan H?ck <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Escaping special characters in text
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Jun 11, 2015 at 03:53:41PM +1000, Thomas Koster wrote:
> My program needs to escape and unescape "special characters" in text
> (Data.Text.Text), using my own definition of "special character"
> (isSpecial :: Char -> Bool). I am looking for a library that provides
> functions that implement or help me implement this functionality. I
> don't really care exactly how the special characters are escaped, but
> my preference is to prefix them with backslashes.
Hi Thomas
The answer to your question depends on whether your program needs
additional functionality. If the only thing you need to do is taking
special characters and escaping them with an escape character plus a
substitute character, this can be done with very little code using
functions from Data.Text:
import Data.Text (Text)
import qualified Data.Text as T
-- Character used for escaping
ec :: Char
ec = '$'
-- Replace a character to be escaped with its substitute
escapeChar :: Char -> Char
escapeChar = id
-- Inverse of escapeChar
unescapeChar :: Char -> Char
unescapeChar = id
-- True if given char needs to be escaped
isSpecial :: Char -> Bool
isSpecial = ('?' ==)
-- Escape chars in a given text
escape :: Text -> Text
escape = T.concatMap handleChar
where handleChar c | isSpecial c = T.pack [ec, escapeChar c]
| otherwise = T.singleton c
-- Unescape chars in a given text
unescape :: Text -> Text
unescape t = case T.break (ec ==) t of
(a,b) | T.null b -> a
| otherwise -> let b' = T.tail b
e = unescapeChar $ T.head b'
in T.append a $
T.cons e $ unescape (T.tail b')
This code was loaded into ghci and tested there, so it should compile
(GHC 7.10).
Example:
escape $ T.pack "This?Is?A?Test??"
yields
"This$?Is$?A$?Test$?$?"
'unescape' yields the original string. Note that the implementation does
not handle trailing escape characters: "This$?Is$?A$" will throw an
exception, but this can be remedied with very little additional code.
You of course must provide the correct implementation for 'ec',
'escapeChar', and 'unescapeChar'. These you need to implement no
matter what other library you use.
If on the other hand you want to escape special characters with blocks of text
(instead of single characters as in my code) you probably also need a
second character to mark the end of an escape. Even then, the code
should not get much more involved than the example above.
Text validation and error handling before unescaping adds some more
bloat, but again should be straight forward to add using Either
as a return type.
So, either this is all you need, or we need more information.
Cheers
Stefan
------------------------------
Message: 5
Date: Thu, 11 Jun 2015 09:21:01 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Packages
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
I?m using Cabal to build a package from some source I?m writing. It is not an
executable but rather a library.
How can I test locally that the package I?m making is complete? ie How do I
reference the package I?ve just buiilt in a haskell source file?
Thanks
Mike
------------------------------
Message: 6
Date: Thu, 11 Jun 2015 14:16:35 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Packages
Message-ID:
<CAJbEW8P3h34a231p1prUomXCUkQNEGWQk+Cykk1_L=12kqp...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Your .cabal file will have some exported-modules. Install the library using
`cabal install` and then import these modules to test them.
Take a look here for more: https://www.haskell.org/cabal/users-guide/
On 11 June 2015 at 13:51, Mike Houghton <[email protected]> wrote:
> I?m using Cabal to build a package from some source I?m writing. It is
> not an executable but rather a library.
> How can I test locally that the package I?m making is complete? ie How do
> I reference the package I?ve just buiilt in a haskell source file?
>
>
> Thanks
> Mike
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150611/55d5a3a7/attachment-0001.html>
------------------------------
Message: 7
Date: Thu, 11 Jun 2015 10:05:18 +0100
From: Mike Houghton <[email protected]>
To: [email protected], The Haskell-Beginners Mailing
List - Discussion of primarily beginner-level topics related to
Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Packages
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Thank you.
> On 11 Jun 2015, at 09:46, Sumit Sahrawat, Maths & Computing, IIT (BHU)
> <[email protected]> wrote:
>
> Your .cabal file will have some exported-modules. Install the library using
> `cabal install` and then import these modules to test them.
>
> Take a look here for more: https://www.haskell.org/cabal/users-guide/
>
> On 11 June 2015 at 13:51, Mike Houghton <[email protected]> wrote:
> I?m using Cabal to build a package from some source I?m writing. It is not
> an executable but rather a library.
> How can I test locally that the package I?m making is complete? ie How do I
> reference the package I?ve just buiilt in a haskell source file?
>
>
> Thanks
> Mike
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> --
> Regards
>
> Sumit Sahrawat
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 84, Issue 19
*****************************************