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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:43:11+00:00 2026-05-29T09:43:11+00:00

Hello All I am trying to flatten a list in Ocaml. I am a

  • 0

Hello All I am trying to flatten a list in Ocaml. I am a newbie so please pardon me if my mistake is dumb

So for example, if input is [[1];[2;3];[4]] I should end up with [1;2;3;4].

The idea I am trying to use is as follows
Iterate through the list from the right (Using fold_right) with accumaltor = []
The pseudo code is as follows

func flatten(list,  accumalator) 
  For each item from right to left in list 
     If Item is a scalar then n :: accumalator
     Else fi Item is a list of form head :: tail then
               head :: flatten (tail, accumalator).

I think that theoretically the algorithm is correct, but please let me know if you disagree.

Now to my OCaml code to implement this algorithm

let rec flatten acc x =
    match x with 
           n -> n :: acc
        | [x] -> x :: acc
        | head :: remainder -> 
            head :: ( my_flat acc remainder ) 
 and my_flat = List.fold_right flatten
 ;;

 my_flat [] [[1];[2;3];[4]]

The Error I get is the following
Error: This expression has type ‘a but an expression was expected of type
‘a list

The error occurs on the line that reads head :: ( my_flat acc remainder ) in the last pattern in the match statement

Any help 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-29T09:43:12+00:00Added an answer on May 29, 2026 at 9:43 am

    In OCaml, all the elements of a list must be the same type. Thus the value [1; [2; 3]; 4] is invalid all by itself. It contains two elements that are of type int and one element of type int list. In essence, your statement of the problem to be solved is impossible.

    $ ocaml312
            Objective Caml version 3.12.0
    
    # [1; [2; 3]; 4];;
    Characters 4-10:
      [1; [2; 3]; 4];;
          ^^^^^^
    Error: This expression has type 'a list
           but an expression was expected of type int
    

    This sounds like a homework problem, so I’ll just say that restricting yourself to lists that are valid in OCaml may make it easier to solve.

    Edit

    OK, the problem can now be solved!

    The essence of the reported type error is something like this. You have your accumulated result acc (of type int list in the example). You want to add the list x (also of type int list) to it. You’ve broken x into head (an int) and remainder (an int list). As you can see, remainder is not a suitable argument for your my_flat function. It wants an int list list, i.e., a list of lists of ints. In fact, your recursive call should almost certainly go to flatten and not to my_flat.

    Another problem I see: the arguments of List.fold_right are: a function, a list, and a starting value. In your test call to my_flat, you’re supplying the last two in the other order. The empty list [] is your starting value.

    I hope this is enough to get you going. Since you’re just starting out with OCaml there will probably be another problem or two before it works.

    Edit 2

    Here are a couple more comments, which might be spoilers if you’re still working on your own solution….

    A tidier version of your function my_flat is in the OCaml standard library under the name List.flatten. It’s interesting to look at the implementation:

    let rec flatten = function
        [] -> []
      | l::r -> l @ flatten r
    

    I’d call this a very elegant solution, but unfortunately it’s not tail recursive. So it will consume some (linear) amount of stack space, and might even crash for a very long list.

    Here’s one based on the same idea, using the standard FP accumulator trick to get tail recursive behavior (as noted by Thomas):

    let flatten2 ll =
        let rec go acc = function
        | [] -> List.rev acc
        | l :: r -> go (List.rev_append l acc) r
    in
        go [] ll
    

    As is often the case, the tail recursive version accumulates the result in reverse order, and reverses it at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello guys I am trying to extract all the anchor links from aol but
Hello every One i am trying to get Ist date of All months in
Hello all Im trying to do a series of when checks to concat two
Hello all i have a button i am trying to let users share a
Currently using Telerik ASP .NET MVC Controls version 2011.2.712 Hello all, I am trying
Hello to all you people out there I have been trying to download the
Hello All , I am trying to use preg_match to identify if a single
I'm trying to replace all occurrence of hello(...) by hello[...] I tried things like
Hello all I'm trying to change the skin (shape) of the button and textArea
Hello all im new to jquery and im trying to make a edit inplace

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.