ghcup install ghc | GHC 설치 |
ghci | 대화형 REPL |
ghc Main.hs -o main | 컴파일 |
runghc script.hs | 스크립트 실행 |
cabal init | 프로젝트 초기화 |
cabal build | 프로젝트 빌드 |
cabal run | 프로젝트 실행 |
stack new myproject | 새 Stack 프로젝트 |
main :: IO ()
main = putStrLn "Hello, World!"
-- With do notation
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello, " ++ name ++ "!") -- Type annotations
x :: Int
x = 42
pi' :: Double
pi' = 3.14159
name :: String
name = "Haskell"
isTrue :: Bool
isTrue = True
-- Type inference
result = x + 10 -- Inferred as Int -- Simple function
add :: Int -> Int -> Int
add x y = x + y
-- Pattern matching
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- Guards
absVal :: Int -> Int
absVal n
| n < 0 = -n
| otherwise = n
-- Where clause
circleArea :: Double -> Double
circleArea r = pi * r ^ 2
where pi = 3.14159 -- Lambda expression
add = \x y -> x + y
-- Higher-order functions
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
-- map, filter, fold
map (*2) [1,2,3] -- [2,4,6]
filter even [1,2,3,4] -- [2,4]
foldr (+) 0 [1,2,3] -- 6
foldl (+) 0 [1,2,3] -- 6
-- Function composition
f . g = \x -> f (g x)
squareSum = sum . map (^2) -- List creation
nums = [1, 2, 3, 4, 5]
chars = ['a', 'b', 'c'] -- Same as "abc"
range = [1..10] -- [1,2,3,4,5,6,7,8,9,10]
even' = [2,4..10] -- [2,4,6,8,10]
-- Cons operator
1 : [2, 3] -- [1,2,3]
'h' : "ello" -- "hello"
-- Concatenation
[1,2] ++ [3,4] -- [1,2,3,4] head [1,2,3] -- 1
tail [1,2,3] -- [2,3]
last [1,2,3] -- 3
init [1,2,3] -- [1,2]
length [1,2,3] -- 3
null [] -- True
reverse [1,2,3] -- [3,2,1]
take 2 [1,2,3] -- [1,2]
drop 2 [1,2,3] -- [3]
elem 2 [1,2,3] -- True
zip [1,2] ['a','b'] -- [(1,'a'),(2,'b')]
zipWith (+) [1,2] [3,4] -- [4,6] [x * 2 | x <- [1..5]] -- [2,4,6,8,10]
[x | x <- [1..10], even x] -- [2,4,6,8,10]
[(x,y) | x <- [1,2], y <- ['a','b']] -- [(1,'a'),(1,'b'),(2,'a'),(2,'b')]
[x + y | x <- [1,2], y <- [10,20], x + y > 11] -- [12,21,22] -- Sum type (enum)
data Color = Red | Green | Blue
-- Product type (record)
data Person = Person {
name :: String,
age :: Int
}
-- Usage
john = Person { name = "John", age = 30 }
john = Person "John" 30
-- Parameterized type
data Maybe a = Nothing | Just a
data Either a b = Left a | Right b
data List a = Empty | Cons a (List a) -- Define typeclass
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
-- Instance
instance Eq Color where
Red == Red = True
Green == Green = True
Blue == Blue = True
_ == _ = False
-- Deriving
data Point = Point Int Int
deriving (Show, Eq, Ord)
-- Common typeclasses: Show, Read, Eq, Ord, Num, Functor, Monad -- Maybe for optional values
safeDivide :: Int -> Int -> Maybe Int
safeDivide _ 0 = Nothing
safeDivide x y = Just (x `div` y)
-- Chaining with >>=
result = Just 10 >>= \x ->
Just 2 >>= \y ->
safeDivide x y -- Just 5
-- Do notation (same as above)
result = do
x <- Just 10
y <- Just 2
safeDivide x y -- IO actions
main :: IO ()
main = do
putStrLn "Enter name:"
name <- getLine
let greeting = "Hello, " ++ name
putStrLn greeting
return ()
-- Useful IO functions
print :: Show a => a -> IO ()
readFile :: FilePath -> IO String
writeFile :: FilePath -> String -> IO ()
getArgs :: IO [String]