Question
Thank you for Question 1 Casfaz!! ! how about second Question. Help me with this one Here is the example A good way to get
Thank you for Question 1 Casfaz!!! how about second Question.
Help me with this one Here is the example
A good way to get the workings of functions, particularly recursive functions, established in ones mind is to walk through the function evaluation step by step. For example, consider the code shown in class for the function to compute the length of a list:
let rec length l =
match l with
| [ ] -> 0
| _::t -> 1 + length t;;
Below are the steps taken in evaluating this function for a particular list:
length [1; 2; 3]
=> 1 + length [2; 3]
=> 1 + (1 + length [3])
=> 1 + (1 + (1 + length [ ]))
=> 1 + (1 + (1 + 0))
=> 1 + (1 + (1))
=> 1 + (2)
=> 3
Now consider an alternate form of the length function:
let rec length_inner l n =
match l with
| [ ] -> n
| h::t -> length_inner t (n + 1);;
let length l = length_inner l 0;;
The steps taken in evaluating this function for the same list as above are:
length [1; 2; 3]
=> length_inner [1; 2; 3] 0
=> length_inner [2; 3] 1
=> length_inner [3] 2
=> length_inner [ ] 3
=> 3
Q2 START HERE
Q2. Consider the function below which reverses the ordering of a list. It was provided by some you in as response to an earlier homework question.
let rev list =
let rec aux acc = function
| [ ] -> acc
| h::t -> aux (h::acc) t in
aux [ ] list;;
Show the step-by-step evaluation of this function for the expression below. Within this evaluation, include the step-by-step evaluation of the embedded helper function as well.
rev [15; 7; 18; 9; 26]
Type out all the steps of the evaluation in the same format as the examples shown on the previous pages.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started