[Haskell-beginners] case statement and guarded equations

PATRICK BROWNE patrick.browne at dit.ie
Fri Jul 7 22:23:25 UTC 2017


In the case statement for the half function I think  variable m is bound to
the value of (snd (half (n - 1)).  With this assumption, I have have
written a guarded version called half1. Is it always possible to write case
statements as a set of conditional equations? My intuition is that this
should be possible and that the guarded version reduces pattern matching?

Thanks,
Pat

data EvenOdd = Even | Odd deriving (Show,Eq)
half 0 = (Even,0)
half n = case half (n-1) of (Even,m) -> (Odd,m)
                            (Odd,m) -> (Even,m+1)

half1 0  =  (Even , 0)
half1 n  |  (fst  (half (n - 1)) == Even)  = (Odd , (snd (half (n - 1))))
half1 n  |  (fst  (half (n - 1)) == Odd)   = (Even , ((snd (half (n - 1)))
+ 1))

-- 


This email originated from DIT. If you received this email in error, please 
delete it from your system. Please note that if you are not the named 
addressee, disclosing, copying, distributing or taking any action based on 
the contents of this email or attachments is prohibited. www.dit.ie

Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí 
earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an 
seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon 
dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa 
ríomhphost nó sna hiatáin seo. www.dit.ie

Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to 
Grangegorman <http://www.dit.ie/grangegorman>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170707/51dbc4da/attachment.html>


More information about the Beginners mailing list