I\'m writing OCaml code that reads in a list and removes any char \'i\'s that ap
ID: 3859003 • Letter: I
Question
I'm writing OCaml code that reads in a list and removes any char 'i's that appear at the beginning of the list. For instance, the list removeI['i';'i';'a';'c';'i'] should return -: int * char list = ['a';'c';'i'], because there are 2 'i's at the beginning of the list. I believe I know how to implement this properly; however, I want to return a tuple that includes the number of removed 'i's as well as the new list with the 'i's removed. I know that may sound confusing, but an example would be removeI['i';'i';'a';'c';'i'] -: int * char list = (2,['a';'c';'i']) There are 2 'i's removed and the new list with the removed 'i's.
So far, I have the following function:
let rec removeI list = match list with
| [] -> []
| x::[] -> x::[]
| x::y::t1 -> if x='i' then removeI (y::t1)
else list;;
This returns the list with the first 'i's removed, but I keep getting errors when I try to include the number of removed 'i's as part of a tuple. Could anyone push me in the right direction? Thanks!
Explanation / Answer
let rec removeI list = match
list with| [] -> []
| x::[] -> x::[]
| x::y::t1 -> if x='i' then removeI (y::t1)
else list;;
------------------
let string_repeat s n =
let len = Bytes.length-1 s in
let res = Bytes.create(n * len) in
for i = 0 to pred n do
Bytes.blit s 0 res (i * len) len
done;
Bytes.to_string res;;
It Gives the signature like
val string_repeat : bytes -> int -> string = <fun>
(Note : Basic ideas ia find number of repeating charaters Length-1, and print (Length-1 count))