Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
pp:2025:l07 [2025/04/14 22:18] cata_chiru [7.5. Recursion and the Sage Bird] |
pp:2025:l07 [2025/04/14 22:36] (current) ldaniel |
||
|---|---|---|---|
| Line 82: | Line 82: | ||
| <hidden Solutions 7.1.1> | <hidden Solutions 7.1.1> | ||
| - | Solutions: | + | Solutions: \\ \\ |
| + | 1. $ \lambda y.(\lambda x.x_1 \ (x_2 \ y_1)) $ \\ | ||
| + | Bounded occurrences: $ x_1 $ (to $ \lambda x $), $ y_1 $ (to $ \lambda y $). \\ | ||
| + | Free occurrences: $ x_2 $ (Not bounded by $ \lambda x.x_1 $) \\ | ||
| + | In the sub-context of $ \lambda x.x_1 \ (x_2 \ y_1) $, $ y_1 $ is free. \\ | ||
| \\ | \\ | ||
| - | + | 2. $ (\lambda x.(x_1 \ \lambda y_1.((x_2 \ y_2) \ z_1)) \ (x_3 \ \lambda y_3.x_4)) $ \\ | |
| - | 1. $ \lambda y.(\lambda x.x_1 \ (x_2 \ y_1)) $ | + | Bounded occurrences: $ x_1, x_2 $ (to $ \lambda x $), $ y_2$ (to $ \lambda y_1 $). \\ |
| - | + | Free occurrences: $ z_1, x_3, x_4 $. \\ | |
| - | Bounded occurrences: $ x_1 $ (to $ \lambda x $), $ y_1 $ (to $ \lambda y $). | + | In the sub-context of $ \lambda y_1.((x_2 \ y_2) \ z_1) $, $ x_2 $ is free. \\ |
| - | + | ||
| - | Free occurrences: $ x_2 $ (Not bounded by $ \lambda x.x_1 $) | + | |
| - | + | ||
| - | In the sub-context of $ \lambda x.x_1 \ (x_2 \ y_1) $, $ y_1 $ is free. | + | |
| \\ | \\ | ||
| - | 2. $ (\lambda x.(x_1 \ \lambda y_1.((x_2 \ y_2) \ z_1)) \ (x_3 \ \lambda y_3.x_4)) $ | + | 3. $ (\lambda f.(\lambda x_1.f_1 \ (x_2 \ x_3)) \ (\lambda x_4.f_2 \ (x_5 \ x_6))) $ \\ |
| - | + | Bounded occurrences: $ f_1 $ (to $ \lambda f $). \\ | |
| - | Bounded occurrences: $ x_1, x_2 $ (to $ \lambda x $), $ y_2$ (to $ \lambda y_1 $), | + | Free occurrences: $ x_2, x_3, f_2, x_5, x_6 $. \\ |
| - | + | ||
| - | Free occurrences: $ z_1, x_3, x_4 $ | + | |
| - | + | ||
| - | In the sub-context of $ \lambda y_1.((x_2 \ y_2) \ z_1) $, $ x_2 $ is free. | + | |
| - | + | ||
| - | \\ | + | |
| - | 3. $ (\lambda f.(\lambda x_1.f_1 \ (x_2 \ x_3)) \ (\lambda x_4.f_2 \ (x_5 \ x_6))) $ | + | |
| - | + | ||
| - | Bounded occurrences: $ f_1 $ (to $ \lambda f $), | + | |
| - | + | ||
| - | Free occurrences: $ x_2, x_3, f_2, x_5, x_6 $ | + | |
| </hidden> | </hidden> | ||
| - | |||
| - | |||
| ===== 7.2. Reduction rules ===== | ===== 7.2. Reduction rules ===== | ||
| Line 199: | Line 185: | ||
| **7.3.3.** Define the $ NOR $ operations over booleans. | **7.3.3.** Define the $ NOR $ operations over booleans. | ||
| - | <hidden Solutions 7.3.1> | + | <hidden Solutions 7.3> |
| Solutions: | Solutions: | ||
| Line 309: | Line 295: | ||
| The **Y**-combinator in lambda calculus looks like this: \\ | The **Y**-combinator in lambda calculus looks like this: \\ | ||
| \\ | \\ | ||
| - | $ FIX = \lambda f.(\lambda x.(f \ (x \ x)) \ (\lambda x.f \ (x \ x))) $ | + | $ FIX = \lambda f.(\lambda x.(f \ (x \ x)) \ \lambda x.(f \ (x \ x))) $ |
| </note> | </note> | ||
| Line 320: | Line 306: | ||
| **7.5.2. (*)** Using the **Y**-Combinator, define a function $ FIB $ that computes the **n**-th fibonacci number. | **7.5.2. (*)** Using the **Y**-Combinator, define a function $ FIB $ that computes the **n**-th fibonacci number. | ||
| - | <hidden Solutions 7.5.1> | + | <hidden Solutions 7.5.> |
| Solutions: | Solutions: | ||
| - | + | \\ \\ | |
| - | FACT = $ \lambda n. Y FACT' n $ | + | 7.5.1 \\ |
| - | + | FACT = $ \lambda n. Y FACT' n $ \\ | |
| - | FACT' = $ \lambda rec. \lambda n. IF (ISZERO n) TRUE (MULT n (rec (PRED n)) -- if (n == 0) return 1 else n * rec(n - 1) $ | + | FACT' = $ \lambda rec. \lambda n. IF (ISZERO n) TRUE (MULT n (rec (PRED n)) -- if (n == 0) return 1 else n * rec(n - 1) $ \\ |
| </hidden> | </hidden> | ||