Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: How to stop program printing Left or Right

Tags:

types

haskell

I've made a calculator in haskell which I run from within GHCi. However since the final number can be an integer or double I've made the type declaration

calc :: String -> Either Integer Double

However the output of the function always has either left or right in front of it for example

Left 7

Right 8.4

Is there a way I can stop the left and right being printed?

like image 453
Joe Avatar asked Dec 02 '22 19:12

Joe


1 Answers

When you evaluate this function, GHCi automatically calls putStrLn . show on the result. It is the show function for Either Integer Double which is adding the Left and Right strings.

To avoid this, you can use either show show instead, which will apply the show function only to the numbers stored inside the Either, so

> putStrLn . either show show $ calc ...

should give you what you want.

like image 160
hammar Avatar answered Dec 19 '22 17:12

hammar