[Haskell-cafe] Proper way to write this

Pupeno pupeno at pupeno.com
Sun Dec 25 23:19:33 EST 2005


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 ?
Thanks.
-- 
Pupeno <pupeno at pupeno.com> (http://pupeno.com)

PS: I have a worse case:
-- | Having a 'DaytimeServer', run it according to its specifications.
runDaytimeServer :: DaytimeServer -> IO DaytimeServer
runDaytimeServer dts =
  case dts of
    DaytimeServer {streamPort = Just _, dgramPort = Just _} -> do
      ss <- runDgramDaytimeServer dts
      ds <- runStreamDaytimeServer dts
      return dts{ss = ss, ds = ds}
    DaytimeServer {streamPort = Just _, dgramPort = Nothing} -> do
      ss <- runStreamDaytimeServer dts
      return dts{ss = ss}
    DaytimeServer {streamPort = Nothing, dgramPort = Just _} -> do
      ds <- runDgramDaytimeServer dts
      return dts{ds = ds}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051226/e8ea63a9/attachment.bin


More information about the Haskell-Cafe mailing list