Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| pp:2024:l09 [2024/04/29 08:37] tpruteanu created | pp:2024:l09 [2024/04/29 15:57] (current) tpruteanu | ||
|---|---|---|---|
| Line 75: | Line 75: | ||
| <code haskell> | <code haskell> | ||
| + | |||
| data BTree = Node Int BTree BTree | Nil | data BTree = Node Int BTree BTree | Nil | ||
| + | |||
| data PrintInfo = PrintInfo { | data PrintInfo = PrintInfo { | ||
| len :: Int, | len :: Int, | ||
| Line 82: | Line 83: | ||
| text :: [String] | text :: [String] | ||
| } | } | ||
| + | |||
| pp :: BTree -> PrintInfo | pp :: BTree -> PrintInfo | ||
| pp Nil = PrintInfo 3 2 ["Nil"] | pp Nil = PrintInfo 3 2 ["Nil"] | ||
| Line 94: | Line 95: | ||
| ntext = aligned_x : center_line : dotted_line : down_lines : combined_lines | ntext = aligned_x : center_line : dotted_line : down_lines : combined_lines | ||
| where | where | ||
| - | aligned_x = replicate (ncenter - (length (show x) `div` 2) - 1) ' ' ++ show x | + | aligned_x = replicate (ncenter - (div (length (show x)) 2) - 1) ' ' ++ show x | 
| center_line = replicate (ncenter - 1) ' ' ++ "|" | center_line = replicate (ncenter - 1) ' ' ++ "|" | ||
| dotted_line = replicate (center pp_l - 1) ' ' ++ | dotted_line = replicate (center pp_l - 1) ' ' ++ | ||
| Line 108: | Line 109: | ||
| zipPad pad f (x:xs) (y:ys) = f x y : zipPad pad f xs ys | zipPad pad f (x:xs) (y:ys) = f x y : zipPad pad f xs ys | ||
| combine l r = l ++ replicate (len pp_l - length l + 1) ' ' ++ r | combine l r = l ++ replicate (len pp_l - length l + 1) ' ' ++ r | ||
| + | |||
| instance Show BTree where | instance Show BTree where | ||
| show = unlines . text . pp | show = unlines . text . pp | ||
| + | |||
| tree :: BTree | tree :: BTree | ||
| tree = Node 1 (Node 2 Nil Nil) (Node 3 Nil Nil) | tree = Node 1 (Node 2 Nil Nil) (Node 3 Nil Nil) | ||
| Line 150: | Line 151: | ||
| -- This is an infinite data type, no way to stop generating the tree | -- This is an infinite data type, no way to stop generating the tree | ||
| data StreamBTree = StreamNode Int StreamBTree StreamBTree | data StreamBTree = StreamNode Int StreamBTree StreamBTree | ||
| + | |||
| + | sbtree = StreamNode 1 sbtree sbtree | ||
| </code> | </code> | ||
| Line 158: | Line 161: | ||
| {- | {- | ||
| - | > sliceTree 2 (repeatTree 3) | + | > sliceTree 2 sbtree | 
| - | 3 | + | 1 | 
| | | | | ||
| --------- | --------- | ||
| |  | | |  | | ||
| - | 3 3 | + | 1 1 | 
| -----  -----  | -----  -----  | ||
| |  |  |  | | |  |  |  | | ||
| Line 269: | Line 272: | ||
| ==== Mathematical Constants ==== | ==== Mathematical Constants ==== | ||
| - | **9.3.4.**  | + | **9.3.4.** Knowing that $math[\displaystyle \lim_{n \rightarrow \infty} \frac{F_{n+1}}{F_n} = \varphi], where $math[F_n] is the nth element of the **Fibonacci** sequence, write an\\ | 
| - | + | ||
| - | Knowing that $math[\displaystyle \lim_{n \rightarrow \infty} \frac{F_{n+1}}{F_n} = \varphi], where $math[F_n] is the nth element of the **Fibonacci** sequence, write an\\ | + | |
| approximation with **tolerance** ''e=0.00001'' of the **Golden Ration** ($math[\varphi]). Use the previously defined ''fibs'' **stream** and the ''select'' function. | approximation with **tolerance** ''e=0.00001'' of the **Golden Ration** ($math[\varphi]). Use the previously defined ''fibs'' **stream** and the ''select'' function. | ||
| Line 280: | Line 281: | ||
| </code> | </code> | ||
| - | **9.3.5.**  | + | **9.3.5.** Consider the //sequence//: | 
| - | + | ||
| - | Consider the //sequence//: | + | |
| $math[a_{n+1} = a_n + sin(a_n)]; where $math[a_0] is an //initial approximation//, randomly chosen (but **not** 0 because $math[a_{n+1} != a_n]). | $math[a_{n+1} = a_n + sin(a_n)]; where $math[a_0] is an //initial approximation//, randomly chosen (but **not** 0 because $math[a_{n+1} != a_n]). | ||
| Line 296: | Line 295: | ||
| ==== Square Root ==== | ==== Square Root ==== | ||
| - | **9.3.6.**  | + | **9.3.6.** Given a number ''k'', we want to create a function which calculates the\\ | 
| - | + | ||
| - | Given a number ''k'', we want to create a function which calculates the\\ | + | |
| **square root** of ''k''. This is another place where **laziness** and **streams** come into play. | **square root** of ''k''. This is another place where **laziness** and **streams** come into play. | ||
| Line 315: | Line 312: | ||
| ==== Derivatives ==== | ==== Derivatives ==== | ||
| - | **9.3.7.**  | + | **9.3.7.** We can approximate the derivative of a function in a certain point using the definition of the derivative: | 
| - | + | ||
| - | We can approximate the derivative of a function in a certain point using the definition of the derivative: | + | |
| $math[\displaystyle f'(a)=\lim_{h \rightarrow 0} \frac{f(a+h)-f(a)}{h}] | $math[\displaystyle f'(a)=\lim_{h \rightarrow 0} \frac{f(a+h)-f(a)}{h}] | ||