
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
Popular Prag Prog topics

Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
New

The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New

Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New

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

First, the code resources:
Page 237: rumbl_umbrella/apps/rumbl/mix.exs
Note: That this file is missing.
Page 238: rumbl_umbrella/app...
New

@noelrappin
Running the webpack dev server, I receive the following warning:
ERROR in tsconfig.json
TS18003: No inputs were found in c...
New

The allprojects block listed on page 245 produces the following error when syncing gradle:
“org.gradle.api.GradleScriptException: A prob...
New

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New

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

Getting an error when installing the dependencies at the start of this chapter:
could not compile dependency :exla, "mix compile" failed...
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

Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New

Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New

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

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:
VM Brasseur
@vmbrasseur
We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

A Ruby-Centric Chat with Noel Rappin @noelrappin
Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /centos
- /ash
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /spring
- /django
- /diversity
- /lua
- /nodejs
- /slackware
- /julia
- /c
- /neovim