<jupyter_start><jupyter_text>4 Defining functions This IHaskell notebook outlines a chapter from the 2nd edition of *Programming in Haskell* by Prof. <NAME>,demonstrating examples and summarising some of the sections.  4.1 New from old<jupyter_code>even n = n `mod` 2 == 0
:type even
splitAt n xs = (take n xs, drop n xs)
:type splitAt
splitAt 3 [1,2,3,4,5,6]
recip n = 1/n
:type recip
recip 2<jupyter_output><empty_output><jupyter_text>4.2 Conditional expressions<jupyter_code>abs n = if n >= 0 then n else -n
:type abs
-- 어떤 수의 부호 (음, 0, 양)
signum n = if n < 0 then -1 else
              if n == 0 then 0 else 1
:type signum<jupyter_output><empty_output><jupyter_text>4.3 Guarded equations<jupyter_code>abs n | n >= 0    = n
      | otherwise = -n
signum n | n < 0     = -1
         | n == 0    = 0
         | otherwise = 1<jupyter_output><empty_output><jupyter_text>4.4 Pattern matching<jupyter_code>not :: Bool -> Bool
not False = True
not True  = False
(&&) :: Bool -> Bool -> Bool
True  && True  = True
True  && False = False
False && True  = False
False && False = False
True  && True  = True
_     && _     = False
True  && b = b
False && _ = False<jupyter_output><empty_output><jupyter_text>Haskell does not permit the same name to be used for more than one argument in a single equation.<jupyter_code>b && b = b
_ && _ = False
b && b' | b == b'   = b
        | otherwise = False<jupyter_output><empty_output><jupyter_text>Tuple patterns<jupyter_code>fst :: (a,b) -> a
fst (x,_) = x

snd :: (a,b) -> b
snd (_,y) = y
fst (1,'a')
snd (1,'a')<jupyter_output><empty_output><jupyter_text>List patterns<jupyter_code>[1,2,3] == 1 : (2 : (3 : []))
head :: [a] -> a
head []     = error "empty list"
head (x:_) = x

tail :: [a] -> [a]
tail []     = []
tail (_:xs) = xs
head [1,2,3]
tail [1,2,3]
tail []<jupyter_output><empty_output><jupyter_text>4.5 Lambda expressions<jupyter_code>:type \x -> x
:type \x -> x + x
(\x -> x + x) 2<jupyter_output><empty_output><jupyter_text>formalize the meaning of curried functions<jupyter_code>add :: Int -> Int -> Int
add x y = x + y
add 3 4
add :: Int -> (Int -> Int)
add =  \x  -> (\y  -> x + y)
(add 3) 4<jupyter_output><empty_output><jupyter_text>more explicit about functions that return functions<jupyter_code>const :: a -> b -> a
const x _ = x
const :: a -> (b -> a)
const x = \_ -> x<jupyter_output><empty_output><jupyter_text>avoid naming functions referenced only once<jupyter_code>odds n = map f [0 .. n-1]
         where f x = x*2 + 1
odds n = map (\x -> x*2 + 1) [0 .. n-1]<jupyter_output><empty_output><jupyter_text>4.6 Operator sections ```haskell (+)   ==   \x -> \y -> x + y (x+)   ==   \y -> x + y (+x)  ==   \x -> x + y```<jupyter_code>evens n = map (\x -> x*2) [0 .. n-1]
evens n = map (*2) [0 .. n-1]
sum = foldr (+) 0
(+3) 1<jupyter_output><empty_output><jupyter_text>----<jupyter_code>:opt no-pager -- to inline :info<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16419965576592083, 'fraction_numerical': 0.02547332185886403, 'mean_word_length': 3.4298780487804876, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '445626', 'license': 'MIT', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1160, 'n_words': 335, 'path': 'pih2ed/4 Defining functions.ipynb', 'repo_name': 'hnu-pl/compiler2019fall', 'size': '2912'}