jrinaldi

jrinaldi

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

@RebeccaSkinner

Currently, it is:

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

It should be:

sumOfUniques n = ​ foldr add additiveIdentity . unique n

Marked As Solved

jrinaldi

jrinaldi

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.

Also Liked

RebeccaSkinner

RebeccaSkinner

Author of Effective Haskell

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:

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:

λ 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:

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

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

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:

-- 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:

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

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
AstonJ
Or looking forward to? :nerd_face:
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in Effective Haskell

Effective Haskell Portal