Home > Enterprise >  How would I return this function?
How would I return this function?

Time:03-30

I want to write a function that takes in a string list that has a 4 letter word, 3 letter word and a five letter word and it returns a list of tuples of three strings.

let word ( str : string list) : (string * string * string) list =
  match str with
  | [] -> []
  | x :: xs -> 
    let helper x = 
      String.length x = 4 
    in 
    List.filter helper str

The code I have so far was to try to filter out the 4 letter words first however I am not sure where to move from here

CodePudding user response:

As you've shown that you really want a function of type:

string list -> string list * string list * string list

Where three, four, and five letter words are filtered out into those three lists, and that you can filter a list to find the four letter words, it should be straightforward to extrapolate to a solution.

let filter words =
  (...,
   List.filter (fun w -> String.length w = 4) words,
   ...)

I leave filling in the ... as an exercise for you.

  • Related