Skip to content

Instantly share code, notes, and snippets.

@werediver
Created July 5, 2013 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werediver/5934005 to your computer and use it in GitHub Desktop.
Save werediver/5934005 to your computer and use it in GitHub Desktop.
Wing aspect ratio calculator in Haskell.
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