[Haskell-cafe] Proper way to write this
Lemmih
lemmih at gmail.com
Mon Dec 26 00:12:42 EST 2005
On 12/26/05, Pupeno <pupeno at pupeno.com> wrote:
> I have this piece of code:
>
> stopDaytimeServer dts =
> case dts of
> DaytimeServer {streamPort = Just _, dgramPort = Just _} -> do
> killServer (ss dts)
> killServer (ds dts)
> DaytimeServer {streamPort = Just _, dgramPort = Nothing} -> do
> killServer (ss dts)
> DaytimeServer {streamPort = Nothing, dgramPort = Just _} -> do
> killServer (ds dts)
>
> and I don't like it. I don't like the code repeting. It'd look good with Lips'
> "when":
> (define stop-daytime-serve dts
> (when (stream dts)
> (kill-server (ss dts)))
> (when (dgram dts)
> (kill-server (ds dts))))
>
> Is there a better way to write that on Haskell ?
Yes, there is:
stopDaytimeServer dts =
do when (isJust (streamPort dts)) $ killServer (ss dts)
when (isJust (dgramPort dts)) $ killServer (ds dts)
You can do the same thing with 'runDaytimeServer'.
--
Friendly,
Lemmih
More information about the Haskell-Cafe
mailing list