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

Use Ocaml language : Write this function in a pure functional style only: no ass

ID: 3788655 • Letter: U

Question

Use Ocaml language : Write this function in a pure functional style only: no assignment statements, no explicit loops, and no arrays!

Here are the problem you have to solve:

Write a simply-recursive (not tail recursive) function to compute factorials (i.e. factorial 10 = 10 * 9 * 8 * ... * 2 * 1). Use an if/then/else statement in the function. Call this function factorial1. NOTE: if you don't know what "simply recursive" or "tail recursive" functions are, then you need to find out before you write this function. Ask the instructor, or do a web search. Have the factorial function handle all numbers from 0 onwards (you don't have to check for less than zero).

Explanation / Answer

# let square x = x * x ;;

val square : int -> int = <fun>

# square 3 ;;

- : int = 9

# let rec fact x=

if x <= 1 then 1 else x* fact (x-1);;

# val fact : int -> int = <fun>

# fact 5 ;;

- : int = 120

# square 120 ;;

- : int = 14400

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote