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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:59:11+00:00 2026-05-20T02:59:11+00:00

Edit: Disregard this question! See comments below. I want an OCaml expression which is

  • 0

Edit: Disregard this question! See comments below.

I want an OCaml expression which is passed a file (as an “in_channel”), then reads the file line by line, doing some processing, to the end, then returns the result of the processing.

I wrote this test:

let rec sampler_string file string_so_far =
    try 
        let line = input_line file in
        let first_two_letters = String.sub line 0 2 in
        sampler_string file (string_so_far ^ first_two_letters)
    with End_of_file -> string_so_far;;

let a = sampler_string (open_in Sys.argv.(1)) "";;

(Here the “doing some processing” is adding the first two characters of each line to a running tally, and the idea is that at the end a string containing the first two characters of every line should be returned.)

This doesn’t work: OCaml thinks that “sampler_string” produces something of type unit, rather than of type string. (Difficulties then occur later when I try to use the result as a string.) I think this problem is because the only base case happens in an exception (the End_of_file).

So, a specific question and a general question:

  1. Is there a way to fix this code, by explicitly telling OCaml to expect that the result of sampler_string should be a string?
  2. Is there some standard, better syntax for a routine which reads a file line by line to the end, and returns the result of line-by-line processing?
  • 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-20T02:59:11+00:00Added an answer on May 20, 2026 at 2:59 am

    As Damien Pollet says, your sampler_string function compiles fine (and runs correctly) on my machine as well, ocaml v3.12.0. However, I’ll answer your questions:

    1. You can specify types on your functions/values using the : operator. For example, here’s your function with it’s types annotated. You’ll notice that the return type is put at the very end of the function declaration.

      let rec sampler_string (file : in_channel) (string_so_far : string) : string = ...
      
    2. I do not know if there’s a better way of reading a file, line-by-line. It certainly is a pain to be forced to deal with an end-of-file via exception. Here’s a blog post on the subject, though the function presented there is of reading a file into a list of lines. Another mailing list version.

    A couple of nitpicks:

    1. You don’t need to use ;; to separate function/value definitions, ocamlc can figure it out from whitespace.
    2. You should close your file sockets.
    3. String.sub will throw an exception if your file has a line with less than 2 characters.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: I have solved this by myself. See my answer below I have set
EDIT: This question is more about language engineering than C++ itself. I used C++
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
UPDATE: This question is outdated, please disregard So.. my idea is to load a
EDIT: upgraded this question to MVC 2.0 With asp.net MVC 2.0 is there an
EDIT: please read the question and why I want to do it before passing
Edit: This is technically a 2 part question. I've chosen the best answer that
EDIT: had to retag this, because, it's rather a Sweave / R question since
EDIT: This question was exceptionally dumb and made me look like a script kiddie,

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.