4

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

I've never played with Haskell before so this took me a relatively long amount of time, although now I see that it is pretty simple.

Anyway, I am required to create interactive Haskell programs for the first two functions and use them to compose and executable with pipes in order to get an equivalent to the third function. I have been reading on pipes and it seems simple enough. My main problem seems to be making the functions interactive.

Any help, tips and resources is greatly appreciated!

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Mark Smith
  • 65
  • 6

1 Answers1

4

The interact function should be pretty useful when solving your problem. Because the UNIX convention is that processes should communicate using text (and not numbers, like temperatures), it means that the interact function wraps functions that take Strings and return Strings. 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 upper-cases all strings that are given 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 this:

echo "bla" | ./uppercase
# Result: "BLA"
dflemstr
  • 25,947
  • 5
  • 70
  • 105
  • Thank you so much for the prompt answer! Sorry if I'm being dense, please bear with me. What I have done is create a separate .hs file for the two first functions (celsiusToKelvin.hs and fahrenheitToCelsius.hs). My understanding is that, with using pipes, I should be able to do : celsiusToKelvin.exe x | fahrenheitToCelsius.exe which should give me the same answer as my third function which was: `fahToKel z = celToKel (fahToCel z)` does the interact function apply? – Mark Smith Jul 15 '12 at 16:17
  • If your program is supposed to use pipes, you cannot do `celsiusToKelvin x | fahrenheitToCelsius`. That would require the `celsiusToKelvin` program to read its input as an argument (which `x` is in that position) instead of from the standard input stream. Instead, you have to use `echo x | celsiusToKelvin | fahrenheitToCelsius`. The built-in `echo` program writes its argument(s) to its standard output stream, which via the pipe then gets directed to the `celsiusToKelvin` program's standard input stream. This would yield the correct results. – dflemstr Jul 15 '12 at 16:21
  • Thank you, this is exactly what I needed. It works perfectly. I got back my assignment marked and I got a perfect score on this question. My problem was that my celsiusToKelvin and FahrenheitToCelsius programs each had strings as prompts for the user, which of course wouldn't work when piping them together. I took those out and it worked! – Mark Smith Jul 25 '12 at 23:13