الرجوع
تسجيل الدخول
أنت تتصفح نسخة مؤرشفة من الموقع اضغط للانتقال إلى الموقع الجديد بكامل المزايا

Here are the 5 hands-on questions along with their answers:

 

---

 

1. **Write a recursive function:**

 

   **Question:**

   Write a recursive function to compute the factorial of a number. Use functional programming principles and show the step-by-step recursion for an input of 5.

 

   **Answer:**

 

   ```

   rec factorial n =

   if iszero n

   then one

   else mult n (factorial (pred n))

   ```

 

   For `factorial 5`:

 

   - factorial 5 = 5 * factorial 4

   - factorial 4 = 4 * factorial 3

   - factorial 3 = 3 * factorial 2

   - factorial 2 = 2 * factorial 1

   - factorial 1 = 1 * factorial 0

   - factorial 0 = 1

 

   So, the final result is:

   ```

   5 * 4 * 3 * 2 * 1 = 120

   ```

 

---

 

2. **Trace recursion:**

 

   **Question:**

   Given the following recursive function for addition:

   ```

   rec add x y =

   if iszero y

   then x

   else add (succ x) (pred y)

   ```

   Trace the function call `add 2 3` step by step.

 

   **Answer:**

 

   For `add 2 3`:

   

   - `add 2 3` → `add (succ 2) (pred 3)` = `add 3 2`

   - `add 3 2` → `add (succ 3) (pred 2)` = `add 4 1`

   - `add 4 1` → `add (succ 4) (pred 1)` = `add 5 0`

   - `add 5 0` → Base case reached, return 5.

 

   So, the result of `add 2 3` is `5`.

 

---

 

3. **Recursive multiplication:**

 

   **Question:**

   Implement the recursive function for multiplying two numbers:

   ```

   rec mult x y =

   if iszero y

   then zero

   else add x (mult x (pred y))

   ```

   Then, compute the result of `mult 3 2` by hand.

 

   **Answer:**

 

   For `mult 3 2`:

 

   - `mult 3 2` → `add 3 (mult 3 1)`

   - `mult 3 1` → `add 3 (mult 3 0)`

   - `mult 3 0` → `zero` (base case)

 

   Now calculate:

   ```

   add 3 (add 3 0) = add 3 3 = 6

   ```

 

   So, the result of `mult 3 2` is `6`.

 

---

 

4. **Subtraction via recursion:**

 

   **Question:**

   Using the following recursive definition of subtraction:

   ```

   rec sub x y =

   if iszero y

   then x

   else sub (pred x) (pred y)

   ```

   Calculate `sub 4 2` step by step and explain what happens if `y` is greater than `x` (e.g., `sub 2 4`).

 

   **Answer:**

 

   For `sub 4 2`:

 

   - `sub 4 2` → `sub (pred 4) (pred 2)` = `sub 3 1`

   - `sub 3 1` → `sub (pred 3) (pred 1)` = `sub 2 0`

   - `sub 2 0` → Base case reached, return `2`.

 

   So, the result of `sub 4 2` is `2`.

 

   For `sub 2 4`:

 

   - `sub 2 4` → `sub (pred 2) (pred 4)` = `sub 1 3`

   - `sub 1 3` → `sub (pred 1) (pred 3)` = `sub 0 2`

   - `sub 0 2` → `sub (pred 0) (pred 2)` = `sub 0 1` → Result is `0` (base case reached).

 

   Explanation: If `y > x`, the subtraction results in `0` because the function continues until `x` reaches zero, decrementing both `x` and `y`.

 

---

 

5. **Recursive power function:**

 

   **Question:**

   Write down the recursive process for calculating `power 2 3` using the function:

   ```

   rec power x y =

   if iszero y

   then one

   else mult x (power x (pred y))

   ```

   Show all the intermediate steps involved in this recursive computation.

 

   **Answer:**

 

   For `power 2 3`:

 

   - `power 2 3` → `mult 2 (power 2 (pred 3))` = `mult 2 (power 2 2)`

   - `power 2 2` → `mult 2 (power 2 (pred 2))` = `mult 2 (power 2 1)`

   - `power 2 1` → `mult 2 (power 2 (pred 1))` = `mult 2 (power 2 0)`

   - `power 2 0` → `1` (base case)

 

 

 Now calculate:

   ```

   mult 2 (mult 2 (mult 2 1)) = mult 2 (mult 2 2) = mult 2 4 = 8

   ```

 

   So, the result of `power 2 3` is `8`.

```