[Haskell-beginners] Help on first program
Brandon Allbery
allbery.b at gmail.com
Fri Mar 28 23:56:44 UTC 2014
On Fri, Mar 28, 2014 at 7:49 PM, John M. Dlugosz
<ngnr63q02 at sneakemail.com>wrote:
> On 3/28/2014 6:14 PM, Brandon Allbery wrote:
>
>> On Fri, Mar 28, 2014 at 7:10 PM, John M. Dlugosz <
>> ngnr63q02 at sneakemail.com
>> <mailto:ngnr63q02 at sneakemail.com>> wrote:
>>
>> Each recursive call is either 1 or (n-1) so it should count down
>> 5,4,3,2,1 and stop
>> the recursion.
>>
>> What am I missing?
>>
>> It doesn't magically stop at 0; Integer (inferred type) is signed.
>> Moreover, even if it
>> were not signed, it would wrap around (or possibly throw an exception on
>> some CPUs, but
>> not on Intel). You need to include a check for 0 to stop the recursion.
>>
>> I don't get it. When n == 1 it should match the second form, and that
> is not recursive.
>
> Ah, they are matched in order!
> (Yes, it works if I reverse the clauses)
Hmm, so it figures out the type from all of them? I worried about putting
> specialized ones first because there is far less information.
It uses all of them to get the type, yes. And the more specific pattern
must come first; the first one always matches in this case because `n`
doesn't give it any way not to match. If you had warnings enabled, the
compiler should have warned you that the second form wouldn't be matched
(although you may also need optimization turned on).
The compiler doesn't see the different implementations as independent, and
in fact doesn't see multiple implementations of the function at all at type
resolution time; it's translated to a single function applying `case` to
the parameters to determine which clause of the body to evaluate.
--
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140328/b0945b62/attachment-0001.html>
More information about the Beginners
mailing list