Creating an interactive program in Haskell

I had to write 3 functions: one to convert Fahrenheit to Celsius, one to convert Celsius to Kelvin and a third that turns Fahrenheit to Kelvin using the first two functions.

I have never played with Haskell, so it took me a relatively long time, although now I see that it is quite simple.

Anyway, I need to create Haskell interactive programs for the first two functions and use them to build and execute using pipes to get the equivalent of the third function. I read on the pipes and it seems simple enough. My main problem is to make interactive functions.

Any help, advice and resources are appreciated!

+4
source share
1 answer

The interact function should be very helpful in solving your problem. Since the UNIX convention is that processes should interact using text (rather than numbers, such as temperature), this means that the interact function wraps functions that take String and return String s. This means that you have to wrap your fahrenheit / celsius functions in new functions that take and return strings instead of numbers.

As an example, to get you started, this program is at the top of all the lines that are assigned to it:

 module Main (main) where import Data.Char (toUpper) main :: IO () main = interact upperCase upperCase :: String -> String upperCase = map toUpper 

You can compile it with:

 ghc uppercase.hs 

... and then you can use it (on Linux) by doing the following:

 echo "bla" | ./uppercase # Result: "BLA" 
+4
source

All Articles