
molleweide
Effective Haskell: Left / Right types in Calculator?
page 141. calculator.
I suggest you explain a little more about why you introduced the Left and Right types in the Calculator parser or where they come from.
If I am correct you use them to distinguish between the first and second output of readEither
but I am not sure.
You do pattern matching against Left / Right in the case statement for readEither in parser'
but it is not clear to me where they come from exactly. When I look at :info
for readEither it sais String -> Either String a
and so I am thinking does readEither return Left/Right values implicitly?
Edit: Now, reading through the code again, I realise that the above is wrong, however, there is something about the source code to the calculator that I don’t really understand.
I see now that the readEither
is used as the case expression in case expr of
but I have never seen a function being used alone as the case expr. Does this mean that lit
is being passed into the case expression and then is passed as argument to readEither
?
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)
Popular Prag Prog topics










Other popular topics










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
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /slackware
- /julia
- /c
- /neovim