<jupyter_start><jupyter_text>6 Recursive functions<jupyter_code>fac :: Int -> Int
fac n = product [1..n]
fac :: Integer -> Integer      -- 수학적 귀납법과 굉장히 밀접한 구조
fac 0 = 1              -- base case
fac n = n * fac (n-1)  -- recursive case
fac 3<jupyter_output><empty_output><jupyter_text>갑자기 궁금해서 해본 것<jupyter_code>parse x xs = xs !! x
parse 0 [1,2,3,4,5]
fac 3
  == {- applying fac -} 6
3 * fac 2
  == {- applying fac -} 6
3 * (2 * fac 1)
  == {- applying fac -} 6
3 * (2 * (1 * fac 0))
  == {- applying fac -} 6
3 * (2 * (1 * 1))
  == {- applying fac -} 6
mult :: Int -> Int -> Int
mult _ 0 = 0
mult n n' = n + (mult n (n'-1))
mult 4 3
  == {- applying mult -} 12
4 + mult 4 2
  == {- applying mult -} 12
4 + (4 + mult 4 1)
  == {- applying mult -} 12
4 + (4 + (4 + mult 4 0))
  == {- applying mult -} 12
4 + (4 + (4 + 0))
  == {- applying mult -} 12
product :: Num a => [a] -> a
product []     = 1 -- base case
product (x:xs) = x * product xs-- recursive case
product [2,3,4]
  == {- applying product -} 24
2 * product [3,4]
  == {- applying product -} 24
2 * (3 * product [4])
  == {- applying product -} 24
2 * (3 * (4 * product [0]))
  == {- applying product -} 24
2 * (3 * (4 * 1))
  == {- applying product -} 24
[2,3,4] == 2 : (3 : (4: []))
length :: [a] -> Int
length [] = 0
length (_:xs) = 1 + (length xs)
length [1,2,3]
reverse :: [a] -> [a]
reverse []     = []
reverse (x:xs) = reverse xs ++ [x]
reverse [1,2,3,4,5]
reverse [1,2,3]
    == {- applying reverse -}     [3,2,1]
reverse [2,3] ++ [1]
   == {- applying reverse -}      [3,2,1]
(reverse [3] ++ [2]) ++ [1]
   == {- applying reverse -}      [3,2,1]
((reverse [] ++ [3]) ++ [2]) ++ [1]
    == {- applying reverse -}     [3,2,1]
(([] ++ [3]) ++ [2]) ++ [1]
   == {- applying reverse -}      [3,2,1]
(++++) :: [a] -> [a] -> [a]
[]     ++++ ys = ys
(x:xs) ++++ ys = x : (xs ++++ ys)
[1,2,3] ++++ [4,5]
    == {- applying ++++ -}     [1,2,3,4,5]
1 : ([2,3] ++++ [4,5])
    == {- applying ++++ -}     [1,2,3,4,5]
1 : (2 : [3] ++++ [4,5])
    == {- applying ++++ -}     [1,2,3,4,5]
1 : (2 : (3 : ([] ++++ [4,5])))
    == {- applying ++++ -}     [1,2,3,4,5]
1 : (2 : (3 : [4,5])) 
    == {- list notation -}     [1,2,3,4,5]
allcase xs = [(x, y) | x <- xs, y <- xs]
allcase [1,2,3,4,5]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2710074791025077, 'fraction_numerical': 0.0840299164100308, 'mean_word_length': 2.3052325581395348, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '433667', 'license': 'MIT', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1041, 'n_words': 280, 'path': 'PiHchap06.ipynb', 'repo_name': '1akewood/haskell-notebook', 'size': '2273'}