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: A basic misunderstanding of how to program with IO
(Isaac Dupree)
2. RE: A basic misunderstanding of how to program with IO
(Ken Overton)
3. Re: A basic misunderstanding of how to program with IO
(Isaac Dupree)
4. Question about FRP trigger based programming (Mathew de Detrich)
5. Haskell Serialization (Tom Hobbs)
6. Re: Haskell Serialization (Stephen Tetley)
7. Re: When, if ever, does Haskell "calculate once"?
(Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Sun, 09 May 2010 18:13:48 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Ken Overton <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 05/08/10 11:55, Ken Overton wrote:
> Sorry for such a beginner-y question, but is there a way to make a function
> like:
>
> interact :: String -> Resp
> interact txt =
> putStrLn txt
> rsp<- getLine
> return parseResp rsp
>
> parseResp :: String -> Resp
>
> Or is that simply a wrong way of programming in Haskell with IO?
I think, yes your function looks close to typical Haskell, you're just
missing a "do", a pair of parentheses, and an "IO":
interact :: String -> IO Resp
interact txt = do
putStrLn txt
rsp <- getLine
return (parseResp rsp)
parseResp :: String -> Resp
Does that make sense to you? Would you like more detailed explanation
of the changes?
-Isaac
------------------------------
Message: 2
Date: Sun, 9 May 2010 19:30:47 -0400
From: Ken Overton <[email protected]>
Subject: RE: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Isaac Dupree <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
> I think, yes your function looks close to typical Haskell, you're just
> missing a "do", a pair of parentheses, and an "IO":
Sorry, I'd definitely intended the 'do' to be there.
> interact :: String -> IO Resp
Thanks Isaac; so after I've called this how do I get the Resp value back out of
the returned IO?
Regards,
--kov
------------------------------
Message: 3
Date: Sun, 09 May 2010 20:12:34 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Ken Overton <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 05/09/10 19:30, Ken Overton wrote:
>
>> I think, yes your function looks close to typical Haskell, you're just
>> missing a "do", a pair of parentheses, and an "IO":
>
> Sorry, I'd definitely intended the 'do' to be there.
>
>> interact :: String -> IO Resp
>
> Thanks Isaac; so after I've called this how do I get the Resp value back out
> of the returned IO?
By using it in some other piece of IO. For example,
main :: IO ()
main = do
resp <- interact "hi there!"
let modifiedResp = some function involving resp
...
You see, "IO" lets you distinguish between functions/values that require
IO actions, and those that are completely pure (always give the same
result, given the same arguments, as is typical in mathematics). Just
use your patience! : Every function that uses that resulting Resp will
have to be in IO. This is not as bad as it seems, because most of the
processing of the Resp can be pure again: for example, the way you call
parseResp which is a pure function, or "some function involving resp" in
my example above.
i'd suggest
1. Practice. The typechecker will tell you when you get it wrong.
2. Generally try to mark less of your code as "IO", because this is good
style ( -- it turns out to be easier to code [and refactor code] with
pure functions, because you know they don't have side-effects, so you
mostly don't need to worry about "when" or "how often" they're called.)
-Isaac
------------------------------
Message: 4
Date: Mon, 10 May 2010 16:01:44 +1000
From: Mathew de Detrich <[email protected]>
Subject: [Haskell-beginners] Question about FRP trigger based
programming
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Basically Im designing an interface which will (eventually) be customizable
through scripts run through HSPlugin, and the scripting is going to be very
reactive based (its for a game). Im basically trying to make a Trigger based
API, so this example should show simply what I am trying to do (you can also
see it here http://pastebin.com/ZNCD75KN). Obviously some of the haskell
isn't legitimate (I just hope im conveying what I am trying to do)
data Event = OnPress | OnRelease
data Bindings = (a,b,..z) -- This isn't legal haskell, but basically a tuple
with any
-- amount of types, this would have to be done with generics or something
along those lines
onKey :: Event Char Bindings (Char â Bindings â ReturnVal)
onKeyPress = error "stub"
testFunc :: Char â Bindings â ReturnVal
testFunc a _ = do
putStrLn [a]
testFunc2 :: Char â Bindings â ReturnVal
testFunc2 a _ = do
putStrLn [z]
where z = a++a
testFunc3 :: Char â Bindings â ReturnVal
testFunc3 a b = do
putStrLn ([a] ++ show bX ++ bZ)
where
bX = b.x -- x binding from the tuple (x,z)
bZ = b.z -- z binding from the tuple (x,z)
main = do
onKey Press 'A' Nothing testFunc
onKey Release 'B' Nothing testFunc2
onKey Press 'C' (x,z) testFunc3
where
x = 2
z = "cheese"
Basically the premise is that you make "Triggers" which are based on 2 data
types. One being the event (i.e. in this example a key being pressed down or
up) and the actual data type that is being triggered (in this case a key).
In the example above, the onKey is the actual function that the users
creating the scripts will use (so it has to be as clear as possible) and the
testFuncX are the functions that the users will create to bind onto the
triggers.
What happens is you attach functions to those triggers, and those functions
execute (for now consecutively but it can be changed) whenever those events
happen. There also needs to be the ability of binding variables to those
functions if you need them (this is what happens for testFunc3 with (x,y)),
in case users need to send extra data to the functions being bounded onto
the triggers. The supposed above would output this
A
BB
C2cheese
I have been reading through two FRP libraries (both Yampa and Reactive)
however the papers are very generic and there seems to be limited examples
(at least in the area of binding functions to triggers instead of the more
generic events and behaviors based on values that change over time). Would
the basic principle above be able to coded using either Reactive or Yampa or
would I have to create my own library to do such a thing, and more
importantly how would I be able to do it (and a working version of the
example above)
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100510/742f8513/attachment-0001.html
------------------------------
Message: 5
Date: Mon, 10 May 2010 09:11:49 +0100
From: Tom Hobbs <[email protected]>
Subject: [Haskell-beginners] Haskell Serialization
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi all,
Can anyone recommend any articles relating to de/serialization in Haskell?
I've been reading these:
-
http://hackage.haskell.org/packages/archive/binary/0.4.1/doc/html/Data-Binary.html
- http://en.wikipedia.org/wiki/Serialization#Haskell
- http://www.haskell.org/tutorial/stdclasses.html (The Read/Show bits)
...and I'd like some more examples etc.
My situation is basically this, I have a non-Haskell black-box tuple space (
http://en.wikipedia.org/wiki/Tuple_space) which I want to use Haskell to
read/write data from/into. I had trouble understanding how Read/Show could
help me here. Data.Binary made more sense, but like I say, I'd really like
to find some more articles and examples.
Even apart from the tuple space stuff. I'm trying to piece together in my
mind how I'd read some serialized data from anywhere and turn that into a
Haskell data type in a nicely reusable way.
I've been flicking through Real World Haskell and couldn't see much that I
found helpful for this.
Many thanks,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100510/e27743ad/attachment-0001.html
------------------------------
Message: 6
Date: Mon, 10 May 2010 11:18:51 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Haskell Serialization
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Tom
If you are interfacing with non-Haskell binary objects - you will want
binary parsing / writing rather than simple serialization as the
format will be determined by the foreign objects.
You can still use Data.Binary (indeed its probably the best choice),
but you will want to use the modules Data.Binary.Get and
Data.Binary.Put directly and probably avoid the Binary class as it is
specialized to serializing values for Haskell only.
There are probably quite a few libraries on Hackage that you can look
at for examples, though there might be more packages that supply
parsers only and don't do writing, e.g:
http://hackage.haskell.org/package/pecoff (Parser only)
There will be more among the packages this list that directly depend on Binary:
http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/binary-0.5.0.2#direct
Best wishes
Stephen
------------------------------
Message: 7
Date: Mon, 10 May 2010 15:21:42 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] When, if ever, does Haskell
"calculate once"?
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, May 6, 2010 at 9:37 PM, Brent Yorgey <[email protected]> wrote:
>> data MyList a = MyList {mylist::[a], mylength::Int}
>
> There's no magic going on here, if you call a function to compute some
> complicated feature of a data structure multiple places in your code,
> it will be computed multiple times, just like in any other language.
> Caching the features you need as in the above example is a good idea
> if the data structures won't change often, and you really do need the
> features many times.
Notice that if you don't need mylength anywhere in your code, it won't
be computed (except if you made your smart constructor extra-strict)
so it's way easier in Haskell to implement the "compute this only once
but only if I really need it" scheme that you often see in other
languages.
--
Jedaï
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 23, Issue 12
*****************************************