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 6798917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:47:13+00:00 2026-05-26T18:47:13+00:00

There are some text files which needed to be manipulated line by line. I

  • 0

There are some text files which needed to be manipulated line by line. I wrote a withFile function as below:

let withFile fn handle =
    let rec iter_lines fh =
    try
        handle (input_line fh);
        iter_lines fh
    with _ -> close_in fh in
    iter_lines (open_in fn);;

So i can manipulate each file as :

withFile "file1.txt" (fun line -> (*...*))
withFile "file2.txt" (fun line -> (*...*))
...

But I am not sure how to exit elegantly when i do not want to handle all lines. For example:

withFile "file3.txt" (fun line ->
   (*when the line meets some condition, i will exit without handling other lines*)
);

Any suggestion is appreciated !

  • 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-05-26T18:47:14+00:00Added an answer on May 26, 2026 at 6:47 pm

    Your function iter_lines isn’t tail recursive, which means you will probably exhaust your stack space if you process a very large file this way. The reason it isn’t tail recursive is that it has to establish and tear down the try ... with mechanism for catching exceptions.

    Other than that, this looks really good to me. This kind of higher-order function is what OCaml (and FP) is all about.

    One way to make it tail recursive is to move the exception handling out to the containing function. I would also be more specific about the exception that you want to handle. So you get this:

    let withFile fn handle =
        let rec iter_lines fh =
            handle (input_line fh);
            iter_lines fh
        in
        let fh = open_in fn in
        try iter_lines fh
        with End_of_file -> close_in fh
    

    If you want to be able to exit early, one easy way is to have your handle function return a boolean that tells whether to continue processing lines or not. You would end up with something like this:

    let withFile fn handle =
        let rec iter_lines fh =
            if handle (input_line fh) then
                iter_lines fh
        in
        let fh = open_in fn in
        try iter_lines fh
        with End_of_file -> close_in fh
    

    If you want to be able to use an exception to exit early, you’d need to catch all exceptions in withFile, close the file, then re-raise any exception other than End_of_file. This gives you code that looks like this:

    let withFile fn handle =
        let rec iter_lines fh =
            handle (input_line fh);
            iter_lines fh
        in
        let fh = open_in fn in
        try iter_lines fh
        with e ->
            (close_in fh; if e <> End_of_file then raise e)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are some text files(Records) which i need to access using C#.Net. But the
There is some html text stored in the Database which I want to show
I'm making a form which consist in some text and uploading files. The problem
I'm creating an application, which has to get some data from text files. The
I am working on application which will create some text files into the SD
Supposed a text file named name.txt . there are some contents as the following
I am trying to write some data from these vectors onto text files. When
According to the text in avcodec.h file, there are some decoders may support multiple
I will show the problem with a example. There is some text in the
Is there some way I can take text (retrieved from a form), and email

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.