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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:57:18+00:00 2026-05-22T02:57:18+00:00

How can I flatten a nested list like this: [1, 2, 3, 4] ==

  • 0

How can I flatten a nested list like this:

[1, 2, 3, 4] == flatten [[[1,2],[3]],[[4]]]
  • 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-22T02:57:19+00:00Added an answer on May 22, 2026 at 2:57 am

    Since nobody else has given this, it is possible to define a function which will flatten lists of an arbitrary depth by using MultiParamTypeClasses. I haven’t actually found it useful, but hopefully it could be considered an interesting hack. I got the idea from Oleg’s polyvariadic function implementation.

    {-# LANGUAGE MultiParamTypeClasses, OverlappingInstances, FlexibleInstances #-}
    
    module Flatten where
    
    class Flatten i o where
      flatten :: [i] -> [o]
    
    instance Flatten a a where
      flatten = id
    
    instance Flatten i o => Flatten [i] o where 
      flatten = concatMap flatten
    

    Now if you load it and run in ghci:

    *Flatten> let g = [1..5]
    *Flatten> flatten g :: [Integer]
    [1,2,3,4,5]
    *Flatten> let h = [[1,2,3],[4,5]]
    *Flatten> flatten h :: [Integer]
    [1,2,3,4,5]
    *Flatten> let i = [[[1,2],[3]],[],[[4,5],[6]]]
    *Flatten> :t i
    i :: [[[Integer]]]
    *Flatten> flatten i :: [Integer]
    [1,2,3,4,5,6]
    

    Note that it’s usually necessary to provide the result type annotation, because otherwise ghc can’t figure out where to stop recursively applying the flatten class method. If you use a function with a monomorphic type that’s sufficient however.

    *Flatten> :t sum
    sum :: Num a => [a] -> a
    *Flatten> sum $ flatten g
    
    <interactive>:1:7:
        No instance for (Flatten Integer a0)
          arising from a use of `flatten'
        Possible fix: add an instance declaration for (Flatten Integer a0)
        In the second argument of `($)', namely `flatten g'
        In the expression: sum $ flatten g
        In an equation for `it': it = sum $ flatten g
    *Flatten> let sumInt = sum :: [Integer] -> Integer
    *Flatten> sumInt $ flatten g
    15
    *Flatten> sumInt $ flatten h
    15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I do the Ruby method Flatten Ruby Method in C#. This method
Using Scala 2.7.7: If I have a list of Options, I can flatten them
how can i write this command from mathematica to matlab? total = Apply[Plus, Flatten[mlat]]/L2
I've got a comment tree nested in the document, using mongoid embeds_many_recursively like this:
First I have an array like: arr = [[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]] I can 'flatten' it using
I have had a look at this Flatten LINQ collection object with nested object
I would like to flatten arbitrary deeply nested collections/structures of elements of some type
How can I flatten the 2 dimensions array int originalArray[][] to 1 dimension array?
Is there an easy way with LINQ to flatten an XML file? I can
Can you cast a List<int> to List<string> somehow? I know I could loop through

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.