Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:27:46+00:00 2026-06-13T09:27:46+00:00

I am solving the Programming assinment for Harvard CS 51 programming course in ocaml.

  • 0

I am solving the Programming assinment for Harvard CS 51 programming course in ocaml.
The problem is to define a function that can compress a list of chars to list of pairs where each pair contains a number of consequent occurencies of the character in the list and the character itself, i.e. after applying this function to the list [‘a’;’a’;’a’;’a’;’a’;’b’;’b’;’b’;’c’;’d’;’d’;’d’;’d’] we should get the list of [(5,’a’);(3,’b’);(1,’c’);(4,’d’)].
I came up with the function that uses auxiliary function go to solve this problem:

let to_run_length (lst : char list) : (int*char) list =
  let rec go i s lst1 =
    match lst1 with
      | [] -> [(i,s)]
      | (x::xs) when s <> x ->  (i,s) :: go 0 x lst1
      | (x::xs) -> go (i + 1) s xs
        in match lst with
          | x :: xs -> go 0 x lst
          | [] -> []

My question is: Is it possible to define recursive function to_run_length with nested pattern matching without defining an auxiliary function go. How in this case we can store a state of counter of already passed elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:27:47+00:00Added an answer on June 13, 2026 at 9:27 am

    The way you have implemented to_run_length is correct, readable and efficient. It is a good solution. (only nitpick: the indentation after in is wrong)

    If you want to avoid the intermediary function, you must use the information present in the return from the recursive call instead. This can be described in a slightly more abstract way:

    • the run length encoding of the empty list is the empty list
    • the run length encoding of the list x::xs is,
      • if the run length encoding of xs start with x, then …
      • if it doesn’t, then (x,1) ::run length encoding of xs

    (I intentionally do not provide source code to let you work the detail out, but unfortunately there is not much to hide with such relatively simple functions.)

    Food for thought: You usually encounter this kind of techniques when considering tail-recursive and non-tail-recursive functions (what I’ve done resembles turning a tail-rec function in non-tail-rec form). In this particular case, your original function was not tail recursive. A function is tail-recursive when the flows of arguments/results only goes “down” the recursive calls (you return them, rather than reusing them to build a larger result). In my function, the flow of arguments/results only goes “up” the recursive calls (the calls have the least information possible, and all the code logic is done by inspecting the results). In your implementation, flows goes both “down” (the integer counter) and “up” (the encoded result).

    Edit: upon request of the original poster, here is my solution:

    let rec run_length = function
      | [] -> []
      | x::xs ->
        match run_length xs with
          | (n,y)::ys when x = y -> (n+1,x)::ys
          | res -> (1,x)::res
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
I'm currently solving a programming problem to enhance my skills (I'm still a newbie)
I'm reviewing a programming problem from a local programming contest. You can download the
I am solving a programming problem which is stuck at calculating nCr efficiently and
I was solving a programming problem on a site. On my machine (Visual Studio
I watched Dynamic Programming - Kapsack Problem (YouTube) . However, I am solving a
I was solving a Project Euler problem that goes as follows: By considering the
There are many problems that can be solved using Dynamic programming e.g. Longest increasing
While solving any programming problem, what is your modus operandi ? How do you
I was solving a programming problem, which wants to find the SYMMETRIC DIFFERENCE between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.