Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Say I have a function let MyFunction x y z= x+y+z I want to keep y and z fixed,

ID: 3635372 • Letter: S

Question

Say I have a function

let MyFunction x y z= x+y+z



I want to keep y and z fixed, and loop and apply myFunction with x being the result of myFunction from the previous run. So, I want to create another function that takes x y z and an integer n, apply myFunction to x y and z, then take the result and set it as x, keep y and z the same as the initial input and "loop" this n-1 times, and give a result that is a float.

lets say x=1, y=2, z=1, n=4

The new function should run like the following
1. 1+2+1=4
2. 4+2+1=7
3. 7+2+1=10
4. 10+2+1=13
It will then return 13

I hope this makes sense. Thanks in advance for any help!

Explanation / Answer

Consider the following example, my function rSum evaluating the sum in recursive fashion. Compiled under VS 2010. Learning F# keep in mind that it's ML based functional language and it is often useful here to think in inductive fashion, like assuming u know how to solve problem for k, then what u do is u r implementing how to solve it for k+1. Here, look carefully on the rSum implementation. All the arguments stay 'fixed', also I think float result is not needed here, as type convertion which F# does is int -> int -> int -> int -> int = int . Remember that functional languages are type inferent. Hope this helps. open System let MyFunction x y z= x + y + z let rec rSum x y z n = if n = 1 then MyFunction x y z else y + z + (rSum x y z (n-1)) let Result = rSum 1 2 1 4 printfn "Result = %d" Result but if u r need the float version: open System let MyFunction x y z= x + y + z let rec rSum (x:float) (y:float) (z:float) n = if n = 1 then MyFunction x y z else y + z + (rSum x y z (n-1)) let Result = rSum 1.0 2.0 1.0 4 printfn "Result = %f" Result