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 Prag Prog 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
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
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
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
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 PragProg

View all threads ❯