Other "Bob" solutions.
module Bob (responseFor) where
import qualified Data.Char as C
import qualified Data.List as L
responseFor :: String -> String
responseFor str
| question && yelling = "Calm down, I know what I'm doing!"
| silent = "Fine. Be that way!"
| question = "Sure."
| yelling = "Whoa, chill out!"
| otherwise = "Whatever."
where
trim = L.dropWhileEnd C.isSpace . L.dropWhile C.isSpace
sanitized = trim str
alphas = filter C.isAlpha $ sanitized
silent = null $ sanitized
question = not silent && last sanitized == '?'
speaking = not silent && not (null alphas)
yelling = speaking && all C.isUpper alphas
Other "Collatz Conjecture" solutions.
module CollatzConjecture (collatz) where
collatz' :: Integer -> Integer
collatz' 1 = 0
collatz' n =
let next = if even n then quot n 2 else 3 * n + 1
in succ $ collatz' next
collatz :: Integer -> Maybe Integer
collatz n
| n <= 0 = Nothing
| otherwise = Just $ collatz' n
Other "Hello World" solutions.
module HelloWorld
( hello
) where
hello :: String
hello = "Hello, World!"
Other "Leap" solutions.
module LeapYear
( isLeapYear
) where
bind :: (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b
bind f a b x = f (a x) (b x)
infixl 5 &&$
(&&$) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
(&&$) = bind (&&)
infixl 5 ||$
(||$) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
(||$) = bind (||)
isLeapYear :: Integer -> Bool
isLeapYear = isDivisibleBy 400 ||$ (isDivisibleBy 4 &&$ not . isDivisibleBy 100)
where
isDivisibleBy = (\divisor x -> mod x divisor == 0)
Other "Pangram" solutions.
module Pangram
( isPangram
) where
import qualified Data.Char as Char
import Data.List
isPangram :: String -> Bool
isPangram = null . (['a' .. 'z'] \\) . map Char.toLower
Other "Rna Transcription" solutions.
module DNA (toRNA) where
import qualified Data.Map as M
translation :: M.Map Char Char
translation =
M.fromList
[ ('G', 'C'),
('C', 'G'),
('T', 'A'),
('A', 'U')
]
toRNA :: String -> Either Char String
toRNA =
mapM translate
where
translate dna = case M.lookup dna translation of
Just rna -> Right rna
Nothing -> Left dna
Other "Space Age" solutions.
module SpaceAge
( Planet(..)
, ageOn
) where
data Planet
= Mercury
| Venus
| Earth
| Mars
| Jupiter
| Saturn
| Uranus
| Neptune
earthAgeSeconds :: Float
earthAgeSeconds = 31557600
relativeOrbit :: Planet -> Float
relativeOrbit planet =
case planet of
Mercury -> 0.2408467
Venus -> 0.61519726
Earth -> 1.0
Mars -> 1.8808158
Jupiter -> 11.862615
Saturn -> 29.447498
Uranus -> 84.016846
Neptune -> 164.79132
ageOn :: Planet -> Float -> Float
ageOn planet seconds = seconds / earthAgeSeconds / relativeOrbit planet