# Effective Haskell: wrong definition of sumOfUniques (page 393 on Kindle)

**URL:** <https://forum.devtalk.com/t/effective-haskell-wrong-definition-of-sumofuniques-page-393-on-kindle/136358>\
**Category:** PragProg Customers\
**Tags:** book-effective-haskell, errata\
**Created:** [17 December 2023 19:57 UTC](https://forum.devtalk.com/t/effective-haskell-wrong-definition-of-sumofuniques-page-393-on-kindle/136358 "2023-12-17T19:57:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrinaldi](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jrinaldi/32/5617_2.png) [@jrinaldi](https://forum.devtalk.com/u/jrinaldi)\
**Post date:** [17 December 2023 19:57 UTC](https://forum.devtalk.com/t/effective-haskell-wrong-definition-of-sumofuniques-page-393-on-kindle/136358/1 "2023-12-17T19:57:24Z")

</div>

@RebeccaSkinner

Currently, it is:

`sumOfUniques n = ​ foldr (add n) (additiveIdentity n) . unique n`

It should be:

`sumOfUniques n = ​ foldr add additiveIdentity . unique n`

---

<div class="post-metadata">

**Author:** ![RebeccaSkinner](https://forum.devtalk.com/user_avatar/forum.devtalk.com/rebeccaskinner/32/3888_2.png) [@RebeccaSkinner](https://forum.devtalk.com/u/RebeccaSkinner)\
**Post date:** [18 December 2023 20:29 UTC](https://forum.devtalk.com/t/effective-haskell-wrong-definition-of-sumofuniques-page-393-on-kindle/136358/2 "2023-12-18T20:29:02Z")

</div>

Hi!

Are you seeing an error? I believe this example should be correct. Remember than in example we’re using our own definition of `Natural` - a record that contains functions that tell us how to do arithmetic:

```haskell
data Natural a = Natural
{ equal :: a -> a -> Bool
, add : a -> a -> a
, multiply :: a -> a -> a
, additiveIdentity :: a
, multiplicativeIdentity :: a
, displayAsString :: a -> String
}

```

We’ll call `sumOfUniques` by giving it some particular definition of natural numbers, for example `intNatural` or `peanoNatural`. Here’s an example of how we might call it:

```auto
λ sumOfUniques intNatural [1..10]
55

```

In this example, we’re passing in `intNatural`, which tells us how to do these operations on `Int` values. Its’ defined like this:

```haskell
intNatural :: Natural Int
intNatural = Natural
  { equal = (==)
  , add = (+)
  , multiply = (*)
  , additiveIdentity = 0
  , multiplicativeIdentity = 1
  , displayAsString = show
  }

```

If we substitute `intNatural` for `n`n in our example, we’d end up with something like this:

```haskell
foldr (add intNatural) (additiveIdentity intNatural) . unique intNatural

```

You can see here that we’re accessing the functions inside of our `Natural` record and passing them in as arguments to `foldr`. If we expaned this out another layer, it’ll make a bit more sense:

```haskell
-- First, remember that
add intNatural = (+)
additiveIdentity intNatural = 0

-- so
foldr (add intNatural) (additiveIdentity intNatural) . unique intNatural
-- is the same as
foldr (+) 0 . unique intNatural

```

Later, once we refactor `Natural` to be a type class instead of a record, we won’t need to explicitly pass around `n` at all, since that will be part of the constraint in our type class. In that case, we can imagine that our definition would be something like:

```haskell
sumOfUniques :: Natural n => [n] -> n
sumOfUniques = foldr add additiveIdentity . unique

```

---

<div class="post-metadata">

**Author:** ![jrinaldi](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jrinaldi/32/5617_2.png) [@jrinaldi](https://forum.devtalk.com/u/jrinaldi)\
**Post date:** [18 December 2023 22:40 UTC](https://forum.devtalk.com/t/effective-haskell-wrong-definition-of-sumofuniques-page-393-on-kindle/136358/3 "2023-12-18T22:40:28Z")

</div>

Hi Rebecca,

Thank you very much for the detailed explanation. Sorry, the example is actually correct.

I was missing the fact that `equal` and `additiveIdentity` are actually **field selectors**. Instead, I thought they were the fields: types `a -> a -> Bool` and `a`, respectively. That would have been the case if the `Natural a` would have been pattern-matched against `Natural{..}` by enabling the `RecordWildCards` extension.
