Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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: recursively building lists (Edward Z. Yang)
2. using the current time (Torsten Otto)
3. Re: recursively building lists (Torsten Otto)
4. Re: using the current time (Edward Z. Yang)
5. Re: using the current time (Daniel Fischer)
6. Re: Parallel mutation (Jon Harrop)
7. Re: text file to data type (Heinrich Apfelmus)
8. scope for variables (legajid)
9. Re: scope for variables (Maciej Piechotka)
10. Re: Parallel mutation (Erik de Castro Lopo)
----------------------------------------------------------------------
Message: 1
Date: Tue, 08 Dec 2009 18:01:14 -0500
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] recursively building lists
To: Torsten Otto <[email protected]>, beginners
<[email protected]>
Message-ID: <1260313258-sup-3...@ezyang>
Content-Type: text/plain; charset=UTF-8
Excerpts from Edward Z. Yang's message of Tue Dec 08 17:45:19 -0500 2009:
> Excerpts from Torsten Otto's message of Tue Dec 08 17:34:30 -0500 2009:
> > testlist 1 = []:[]
> > testlist n = []:(testlist n-1)
>
> Works for me on GHC 6.10.4. What compiler are you using?
Ah, it compiles, but refuses to run. Please ignore me. :-)
Edward
------------------------------
Message: 2
Date: Wed, 9 Dec 2009 00:18:54 +0100
From: Torsten Otto <[email protected]>
Subject: [Haskell-beginners] using the current time
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
Hi!
We are trying to build a chatbot that can among many other things tell the
partner what the current time is. Now obviously current time and pure
functional programming don't mix well, but Haskell CAN do it. We have played
around with it for a while and have figured out quite a bit, but the final
icing is missing on the cake.
It would be cool to have a dictionary like so
> wissen = [
Einfache Begrüßung -> Frage nach dem
Wohlbefinden
> (["hallo", "hi", "hey"], "Hi, wie
> geht's?"),
> (["guten tag", "tag"], "Wie geht es
> dir?")]
(yes, the bot speaks German!) that could include a line like
> (["Uhr","Zeit","spät"], time)
where time is a function evaluating to a String including the current time.
Once we have the time, we can get it translated into German by
> formatiereZeit :: ClockTime -> String
> formatiereZeit t
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Mon") = "Montag"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Tue") = "Dienstag"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Wed") = "Mittwoch"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Thu") = "Donnerstag"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Fri") = "Freitag"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Sat") = "Samstag"
> | (head(words (calendarTimeToString
> (toUTCTime t))) == "Sun") = "Sonntag"
> | otherwise ="keine Uhr heute"
which results in a string. Now how do we access the system time in a useful way?
> time = formatiereZeit (NOW WHAT?)
Would the current time step out of the IO monad, please?
Regards,
Torsten
------------------------------
Message: 3
Date: Wed, 9 Dec 2009 00:25:00 +0100
From: Torsten Otto <[email protected]>
Subject: Re: [Haskell-beginners] recursively building lists
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thank you! I'll go hide in a dark corner...
Torsten
Am 08.12.2009 um 23:43 schrieb Daniel Fischer:
> Am Dienstag 08 Dezember 2009 23:34:30 schrieb Torsten Otto:
>> Hi!
>>
>> While trying to implement "words", we ran into the question of how to build
>> lists of lists. The trouble boils down to this:
>>
>> test = []:[]
>>
>> is no problem, just as
>>
>> Prelude> []:[]:[]:[]
>> [[],[],[]]
>>
>> works fine. Now trying to put the two together _is_ a problem:
>>
>> testlist 1 = []:[]
>> testlist n = []:(testlist n-1)
>>
>> No instance for (Num [[a]])
>> arising from a use of `testlist' at <interactive>:1:0-9
>> Possible fix: add an instance declaration for (Num [[a]])
>> In the expression: testlist 2
>> In the definition of `it': it = testlist 2
>>
>> Can someone please explain, what is going on here?
>
> Yes. Function application has higher precedence than aritthmetic operations,
> so the RHS of
> testlist n is parsed as
>
> [] : ( (testlist n) - 1)
>
> So, by the equation for testlist 1, the compiler knows that
>
> testlist :: (Num n) => n -> [[a]]
>
> In the second equation, you try to subtract 1 from a value of type [[a]],
> that means you
> need a Num instance for that type.
>
> What you want is
>
> testlist n = []:testlist (n-1)
>
> HTH,
> Daniel
------------------------------
Message: 4
Date: Tue, 08 Dec 2009 18:33:30 -0500
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] using the current time
To: Torsten Otto <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1260314942-sup-7...@ezyang>
Content-Type: text/plain; charset=UTF-8
Excerpts from Torsten Otto's message of Tue Dec 08 18:18:54 -0500 2009:
> > time = formatiereZeit (NOW WHAT?)
>
> Would the current time step out of the IO monad, please?
Unfortunately, there is no way to "step out" of the IO monad:
once in the IO monad, always in the IO monad.
However, if you let time be of type IO String instead of String,
you can do:
time = (liftM formatiereZeit) getClockTime
Where liftM :: (a -> b) -> m a -> m b, i.e. it converts
your function ClockTime -> String into IO ClockTime -> IO String.
Cheers,
Edward
------------------------------
Message: 5
Date: Wed, 9 Dec 2009 00:39:38 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] using the current time
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Mittwoch 09 Dezember 2009 00:18:54 schrieb Torsten Otto:
> Hi!
>
> We are trying to build a chatbot that can among many other things tell the
> partner what the current time is. Now obviously current time and pure
> functional programming don't mix well, but Haskell CAN do it. We have
> played around with it for a while and have figured out quite a bit, but the
> final icing is missing on the cake.
>
> It would be cool to have a dictionary like so
>
> > wissen = [
>
> Einfache Begrüßung -> Frage nach dem
> Wohlbefinden
>
> > (["hallo", "hi", "hey"], "Hi, wie
> > geht's?"), (["guten tag", "tag"], "Wie geht es dir?")]
Unnecessarily verbose!!!
(["Moin"], "Und?")
>
> (yes, the bot speaks German!) that could include a line like
>
> > (["Uhr","Zeit","spät"], time)
>
> where time is a function evaluating to a String including the current time.
>
> Once we have the time, we can get it translated into German by
>
> > formatiereZeit :: ClockTime -> String
> > formatiereZeit t
> >
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Mon") =
> > "Montag"
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Tue") =
> > "Dienstag"
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Wed") =
> > "Mittwoch"
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Thu") =
> > "Donnerstag"
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Fri") =
> > "Freitag"
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Sat") =
> > "Samstag"
But that's wrong, it must be "Sonnabend", otherwise you're speaking Austrian or
Bavarian,
not German.
> > | (head(words (calendarTimeToString (toUTCTime t))) == "Sun") =
> > "Sonntag"
> > | otherwise ="keine Uhr heute"
>
> which results in a string. Now how do we access the system time in a useful
> way?
>
> > time = formatiereZeit (NOW WHAT?)
>
> Would the current time step out of the IO monad, please?
Since the chatbot probably works in IO anyway, is it an option to have the
second
components of type IO String in general?
If that is not possible, sacrifice a few virgins, spit over your left shoulder
and
contemplate using unsafePerformIO.
>
> Regards,
> Torsten
------------------------------
Message: 6
Date: Wed, 9 Dec 2009 03:05:45 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Parallel mutation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Tuesday 08 December 2009 21:37:41 Daniel Fischer wrote:
> Am Dienstag 08 Dezember 2009 21:56:45 schrieb Jon Harrop:
> > Can you mutate different parts of the same array from different threads
> > in parallel using GHC? Someone once told me this was possible but I
> > cannot see how...
>
> You can (take a look at Control.Concurrent), but very probably ***You
> really shouldn't***, so I won't elaborate.
>
> What would you want to do that for?
Efficiency, e.g. parallel quicksort.
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
------------------------------
Message: 7
Date: Wed, 09 Dec 2009 11:38:15 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: text file to data type
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Patrick LeBoutillier wrote:
> Heinrich Apfelmus wrote:
>>
>> import Data.ByteString.Lazy.Char8 as B
>>
>> parse = map (zipWith ($) formats . B.split '\t') . B.lines
>> where
>> formats = [str, str, int, int, int, int, int,
>> int, int, float, float]
>> int = fst . fromJust . readInt
>> float = \s -> read (unpack s) :: Double
>> str = id
>>
>
> I've been looking at this example and I can't figure it out how it works.
> Seems to me that "formats" is a list of functions that return different types.
> How does this work?
Oops. It doesn't. :D
Mea culpa, the functions having different types is indeed a problem.
Fortunately, the trick from Oliver Danvy's "Functional Unparsing"
http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf
applies. Here's the code:
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
parse = map (convert format . B.split '\t') . B.lines
where
format = str . str . int . int . int . int
. int . int . int . float . float
int = lift $ fst . fromJust . B.readInt
float = lift $ \s -> read (B.unpack s) :: Double
str = lift $ id
lift :: (a -> b) -> ([a] -> c) -> ([a] -> (b,c))
lift f k (x:xs) = (f x, k xs)
convert k = k nil where nil [] = ()
This time, I've also tested it in GHCi. Try :type parse to see the
magic type.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 8
Date: Wed, 09 Dec 2009 22:40:51 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] scope for variables
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hello,
i wrote the following (very simplified) program :
word :: [Char]
word="initial"
letters=['a'..'z']
myloop = do
myloop2
myloop2 = do
putStrLn word
putStrLn letters
main = do
putStrLn "Enter the word"
word <- enter_word
myloop
enter_word= do
return("abcdefghij")
"word" is initially binded to the value "initial"
myloop, calling myloop2, displays this value
"Improving" my program, i add "main" that enters a new value for "word"
(here a fix one, ideally a getLine)
To display this new value, i must call myloop with this parameter, which
is then passed to myloop2.
Is there a way to make a "global binding ?", thus changing the value of
"word", so that i don't have to modify the code for myloop and myloop2
(now being passed a parameter), that worked fine. This would make the
program modular and easy to modify, without modifying all existing
functions. Or is it non-sense in Haskell ?
Thanks,
Didier.
------------------------------
Message: 9
Date: Wed, 09 Dec 2009 22:13:45 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: scope for variables
To: [email protected]
Message-ID: <1260396822.2949.53.ca...@picard>
Content-Type: text/plain; charset="UTF-8"
On Wed, 2009-12-09 at 22:40 +0100, legajid wrote:
> Hello,
> i wrote the following (very simplified) program :
>
> word :: [Char]
> word="initial"
> letters=['a'..'z']
>
> myloop = do
> myloop2
word from topmost declaration (="initial") if it was used
>
> myloop2 = do
> putStrLn word
word from topmost declaration (="initial")
> putStrLn letters
>
> main = do
> putStrLn "Enter the word"
> word <- enter_word
word="abcdefghij" and is local variable/constant.
> myloop
>
> enter_word= do
> return("abcdefghij")
>
> "word" is initially binded to the value "initial"
> myloop, calling myloop2, displays this value
> "Improving" my program, i add "main" that enters a new value for "word"
> (here a fix one, ideally a getLine)
> To display this new value, i must call myloop with this parameter, which
> is then passed to myloop2.
> Is there a way to make a "global binding ?", thus changing the value of
> "word", so that i don't have to modify the code for myloop and myloop2
> (now being passed a parameter), that worked fine. This would make the
> program modular and easy to modify, without modifying all existing
> functions. Or is it non-sense in Haskell ?
>
It is practically 'non-sense' and would make a program less modular. If
function depends on some value make it an argument.
Generally you should avoid IO as much as possible.
> Thanks,
> Didier.
Regards
PS. Actually it is possible to do it with global variables but:
1. It uses an extentions
2. You should know why in most cases it is not what you want
3. It requires some knoledge about compilation process as
'officially' (w/out extention) it is nondeterministic.
------------------------------
Message: 10
Date: Thu, 10 Dec 2009 11:55:01 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Parallel mutation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Jon Harrop wrote:
> On Tuesday 08 December 2009 21:37:41 Daniel Fischer wrote:
> > Am Dienstag 08 Dezember 2009 21:56:45 schrieb Jon Harrop:
> > > Can you mutate different parts of the same array from different threads
> > > in parallel using GHC? Someone once told me this was possible but I
> > > cannot see how...
> >
> > You can (take a look at Control.Concurrent), but very probably ***You
> > really shouldn't***, so I won't elaborate.
> >
> > What would you want to do that for?
>
> Efficiency, e.g. parallel quicksort.
So why not look at Data Parallel Haskell:
http://www.haskell.org/haskellwiki/GHC/Data_Parallel_Haskell
Erik
--
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 7
****************************************