
molleweide
Effective Haskell: Calculator throws errors in parse'
Allright, so I have put all of the example code for Calculator in chapter 4 on page ~140 in a file and when loading it into GHCI it throws this:
λ: :reload
[1 of 1] Compiling Calculator ( Calculator.hs, interpreted )
Calculator.hs:40:9: error:
• Couldn't match expected type ‘String -> Either String a1’
with actual type ‘Either String b0’
• In the pattern: Left err
In a case alternative: Left err -> Left err
In the expression:
case readEither of
Left err -> Left err
Right lit' -> Right (Lit lit', rest)
|
40 | Left err -> Left err
| ^^^^^^^^
Calculator.hs:41:9: error:
• Couldn't match expected type ‘String -> Either String a1’
with actual type ‘Either a0 Int’
• In the pattern: Right lit'
In a case alternative: Right lit' -> Right (Lit lit', rest)
In the expression:
case readEither of
Left err -> Left err
Right lit' -> Right (Lit lit', rest)
|
41 | Right lit' -> Right (Lit lit', rest)
| ^^^^^^^^^^
Failed, no modules loaded.
I am not sure if maybe there is something that you forgot to add or that should be added.
Below I post my full calculator module:
module Calculator where
import Text.Read (readEither)
data Expr = Lit Int
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
eval :: Expr -> Int
eval expr =
case expr of
Lit num -> num
Add arg1 arg2 -> eval' ( + ) arg1 arg2
Sub arg1 arg2 -> eval' ( - ) arg1 arg2
Mul arg1 arg2 -> eval' ( * ) arg1 arg2
Div arg1 arg2 -> eval' div arg1 arg2
where
eval' :: (Int -> Int -> Int) -> Expr -> Expr -> Int
eval' operator arg1 arg2 = operator (eval arg1) (eval arg2)
parse :: String -> Either String Expr
parse str =
case parse' (words str) of
Left err -> Left err
Right (e, []) -> Right e
Right (_, rest) -> Left $ "Found extra tokens: " <> (unwords rest)
parse' :: [String] -> Either String (Expr, [String])
parse' [] = Left "unexpected end of expression"
parse' (token:rest) =
case token of
"+" -> parseBinary Add rest
"*" -> parseBinary Mul rest
"-" -> parseBinary Sub rest
"/" -> parseBinary Div rest
lit ->
case readEither of
Left err -> Left err
Right lit' -> Right (Lit lit', rest)
parseBinary :: (Expr -> Expr -> Expr) -> [String] -> Either String (Expr, [String])
parseBinary exprConstructor args =
case parse' args of
Left err -> Left err
Right (firstArg, rest') ->
case parse' rest' of
Left err -> Left err
Right (secondArg, rest'') ->
Right $ (exprConstructor firstArg secondArg, rest'')
run :: String -> String
run expr =
case parse expr of
Left err -> "Error: " <> err
Right expr' ->
let answer = show $ eval expr'
in "The answer is: " <> answer
First Post!

RebeccaSkinner
Author of Effective Haskell
Hi, it looks like you’re missing the lit
parameter to readEither
in the parse'
function. The correct version of the function is:
parse' :: [String] -> Either String (Expr, [String])
parse' [] = Left "unexpected end of expression"
parse' (token:rest) =
case token of
"+" -> parseBinary Add rest
"*" -> parseBinary Mul rest
"-" -> parseBinary Sub rest
"/" -> parseBinary Div rest
lit ->
case readEither lit of
Left err -> Left err
Right lit' -> Right (Lit lit', rest)
It looks like this is fixed in the most recent version- thanks for the catch and sorry for the trouble!
Popular Pragmatic Bookshelf topics

Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list)
Typo:...
New

Hello Brian,
I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

Hi @Margaret ,
On page VII the book tells us the example and snippets will be all using Elixir version 1.11
But on page 3 almost the en...
New

Hi! I know not the intentions behind this narrative when called, on page XI:
mount() |> handle_event() |> render()
but the correc...
New

This is as much a suggestion as a question, as a note for others.
Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New

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

Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels.
Before ...
New

Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

New

No chair. I have a standing desk.
This post was split into a dedicated thread from our thread about chairs :slight_smile:
New

Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New

If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New

Author Spotlight
Mike Riley
@mriley
This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New

The overengineered Solution to my Pigeon Problem.
TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New

zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig.
General-purpose programming language and toolchain for maintaini...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /debian
- /nixos
- /sublime-text
- /textmate
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /django
- /manjaro
- /spring
- /diversity
- /lua
- /nodejs
- /slackware
- /c
- /julia
- /markdown