module Main where
import Data.List (sort, group)
import qualified Data.Map as Map
import Control.Monad (when, forM_)
i :: Int
i = 42
d :: Double
d = 3.14
c :: Char
c = 'A'
s :: String
s = "Hello, Haskell!"
b :: Bool
b = True
list :: [Int]
list = [1, 2, 3, 4, 5]
tuple :: (Int, String)
tuple = (1, "One")
data Color
= Red
| Green
| Blue
| RGB Int Int Int
deriving (Show, Eq)
data Maybe a
= Nothing
| Just a
data Person = Person
{ name :: String
, age :: Int
, active :: Bool
} deriving (Show)
class Describable a where
describe :: a -> String
instance Describable Color where
describe Red = "Red color"
describe Green = "Green color"
describe Blue = "Blue color"
describe (RGB r g b) = "RGB(" ++ show r ++ "," ++ show g ++ "," ++ show b ++ ")"
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
analyzeList :: [a] -> String
analyzeList [] = "Empty list"
analyzeList [x] = "Singleton list"
analyzeList (x:xs) = "List with head and tail"
grade :: Int -> String
grade score
| score >= 90 = "A"
| score >= 80 = "B"
| score >= 70 = "C"
| otherwise = "F"
checkZero :: Int -> String
checkZero x = case x of
0 -> "It's zero"
_ -> "Not zero"
circleArea :: Double -> Double
circleArea r = pi * r ^ 2
where
pi = 3.14159
cylinderVolume :: Double -> Double -> Double
cylinderVolume r h =
let area = circleArea r
in area * h
squares :: [Int]
squares = map (\x -> x * x) [1..10]
filtered :: [Int]
filtered = filter (> 5) squares
pairs :: [(Int, Int)]
pairs = [(x, y) | x <- [1..3], y <- [1..3], x /= y]
increment :: Int -> Int
increment = (+ 1)
half :: Double -> Double
half = (/ 2)
customOp :: Int -> Int -> Int
customOp x y = x + y * 2
result = 5 `customOp` 3
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn $ "Hello, " ++ name
when (length name > 5) $ do
putStrLn "That's a long name!"
newtype UserId = UserId Int
type Username = String
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wall #-}
foreign import ccall "math.h sin" c_sin :: Double -> Double
data Expr a where
I :: Int -> Expr Int
B :: Bool -> Expr Bool
Add :: Expr Int -> Expr Int -> Expr Int
Mul :: Expr Int -> Expr Int -> Expr Int
Eq :: Eq a => Expr a -> Expr a -> Expr Bool