[Haskell-cafe] A functor for two Peano systems

Anthony Clayden anthony_clayden at clear.net.nz
Mon Apr 5 23:13:49 UTC 2021


> This whole exercise is my attempt to translate *The Littler MLer* and its
last chapter where they go into functors.

That approach isn't working for you, and it's trying the patience of the
cafe. (These are sub-beginner level q's, please use the Beginners' forum.
And I'd forgotten that's what you're doing. Your first post in this thread
should have said you're talking about ML functors.
https://stackoverflow.com/questions/2030863/in-functional-programming-what-is-a-functor#:~:text=In%20the%20ML%20family%20of,programmers%20have%20difficulty%20with%20it.
says
"most beginning programmers have difficulty with [functors]".)

I suggest you first go through a Haskell tutorial, and get your knowledge
secure of idiomatic Haskell. Even so, I just don't believe you're coming
from idiomatic ML. Unless 'The Little MLer' is giving it as obfuscated
code, and the objective is to de-obfuscate it.

I'd code your `plus2` with pattern-matching:

> plus2 :: MyNum -> MyNum -> MyNum
> plus2 MNZero m = m
> plus2 (OneMoreThan n') m = OneMoreThan $ plus2 n' m

Your test for `(x == MNZero)` inside helper function `pCessor` is useless:
flow-of-control doesn't take the outer `else` branch unless `x` (i.e. `n`)
is _not_ equal `MNZero`.

Similar redundant code in the case for `Int`. But that has bigger problems,
as Olaf points out: `Int`s can be negative. So if `plus` is called with a
negative `n`, it'll call `pCessor` repeatedly until stack overflow (or
numeric underflow).

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210406/35fb33ac/attachment.html>


More information about the Haskell-Cafe mailing list