Question
Hi, I am very confused by this question in OCAML, so if anyone could shed some light on it, it would be great! Thanks! We
Hi, I am very confused by this question in OCAML, so if anyone could shed some light on it, it would be great! Thanks!
We can represent binary numbers as a list of increasing integers where each element is a power of two:
type nat = int list For example: 5 = [1; 4] or 15 = [1; 2; 4; 8] or 17 = [1; 16].
We can represent 0 with the empty list. The sparse representation can be a more useful way of representing numbers than using a dense representation (i.e. one using ones and zeroes), especially for human-readable arithmetic.
1) Implement a function inc : nat -> nat which increments a given sparse binary number in OCAML.
let inc (ws : nat ) : nat =
2) Implement a function dec : nat -> nat which decrements (i.e., subtracts one from) a given sparse binary number. If given 0 as input, you should raise the exception Domain IN OCAML.
let dec (ws : nat) : nat =
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