import Data.List -- 모듈 임포트
numUniques :: (Eq a) => [a] -> Int
numUniques = length . nub
ghci> numUniques [1, 2, 3, 1, 2, 1, 1]
3
ghci> :m + Data.List
ghci> :m + Data.List Data.Map Data.Set -- 여러 개의 모듈들을 한 번에 임포트
import Data.List (nub, sort)
import Data.List hiding (nub)
import qualified Data.Map as M
M.filter ...
# wordNum.hs
import Data.List
wordNum :: String -> [(String, Int)]
wordNum = map (\ws -> (head ws, length ws)) . group . sort . words
-- wordNum xs = map (\ws -> (head ws, length ws)) (group (sort (words xs)))
ghci> wordNum "wa wa wee wa"
[("wa",3),("wee",1)]
# Geometry.hs
module Geometry
( sphereVolume
, sphereArea
, cubeVolume
, cubeArea
) where
sphereVolume :: Float -> Float
sphereVolume radius = (4.0 / 3.0) * pi * (radius ^ 3)
sphereArea :: Float -> Float
sphereArea radius = 4 * pi * (radius ^ 2)
cubeVolume :: Float -> Float
cubeVolume side = rectArea side side * side
cubeArea :: Float -> Float
cubeArea side = rectArea side side * 6
rectArea :: Float -> Float -> Float
rectArea a b = a * b
# Sphere.hs
module Geometry.Sphere
( volume
, area
) where
volume :: Float -> Float
volume radius = (4.0 / 3.0) * pi * (radius ^ 3)
area :: Float -> Float
area radius = 4 * pi * (radius ^ 2)
# Cube.hs
module Geometry.Cube
( volume
, area
) where
volume :: Float -> Float
volume side = side * side * side
area :: Float -> Float
area side = side * side * 6
# Test.hs (Sphere와 Cube를 import하여 사용)
module Test
(
) where
import qualified Geometry.Sphere as S
import qualified Geometry.Cube as CC