Created
July 5, 2013 11:44
-
-
Save werediver/5934005 to your computer and use it in GitHub Desktop.
Wing aspect ratio calculator in Haskell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WingAR where | |
import Data.Maybe | |
import Text.Printf | |
-- See http://en.wikipedia.org/wiki/Aspect_ratio_%28wing%29 | |
wing_ar :: Float -> Float -> Float | |
wing_ar l s = l^2 / s | |
readMaybe s = listToMaybe $ fmap fst $ reads s | |
getFloatMaybe :: IO (Maybe Float) | |
getFloatMaybe = getLine >>= return . readMaybe | |
getFloatForSure :: String -> String -> IO Float | |
getFloatForSure prompt retry = | |
putStr prompt >> getFloatMaybe >>= \mx -> | |
case mx of | |
Nothing -> putStr retry >> getFloatForSure prompt retry | |
Just x -> return x | |
main :: IO () | |
main = | |
let retry = "Incorrect input. Please retry.\n" in | |
do putStr "Wing lambda calculator (in Haskell)\n\n" | |
l <- getFloatForSure "Wing length: " retry | |
s <- getFloatForSure "Wing square: " retry | |
printf "\nWing lambda: %f\n" $ wing_ar l s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment