Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 531

How to divide a list of characters (that represents a word) into all possible (prefix, single_letter, suffix) triplets in CAML?

$
0
0

I defined a type "word" as a list of characters. I am trying to write a recursive function "divide_word" that takes one "word" as an argument and returns a list of triplets of all possible (prefix, single_letter, suffix) combinations of the "word".

Example of execution in OCAML:

assert( divide_word ['c';'o';'d';'e'] = [  ([],'c',['o';'d';'e']) ; (['c'],'o',['d';'e']) ; (['c','o'],'d',['e']) ; (['c','o','d'],'e',[])  ];;

I've tried writing the function, but it only returns the last possible combination. I know that I have to find a way to return an actual list and not just a single triplet, I tried to concatenate and also to write a separate function.

This is the code that I've written:

type word = char list;;let rec divide_word (w:word) : (word*char*word) list =  match w with  | [] -> []  | h::[] -> [([], h, [])]  | h::t -> List.map (fun (fir, sec, th) -> (h::fir, sec, th)) (divide_word t);;

help! c:


Viewing all articles
Browse latest Browse all 531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>