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 Pragprog topics










Other popular topics








Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm