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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:31:30+00:00 2026-06-12T18:31:30+00:00

I am working on a function in Haskell that will take one list and

  • 0

I am working on a function in Haskell that will take one list and divide it into two evenly sized lists. Here is what I have:

split (x:y:xs) = split2 ([((length(x:y:xs) `div` 2)-2) : x ++ y] : [xs])
split2 (x:xs:[y:ys]) = split2 ((x-1) : [xs] ++ y : [ys])
split2 (0:xs:[y:ys]) = (xs:[y:ys])

The function takes the first two elements of a list, and puts them together into list #2 and appends the first list as a second element. It then gets the length of the list, and divides it by two to find out how many times to run taking into account the fact that it already removed two elements from the first list. It then takes those two pieces of information and puts it into split2, which takes another element from the first list and appends it to the second list in the first element, also it counts down 1 from the number of runs and then runs again.

Problem is, when I run it I get this:

Functions.hs:19:49:
    Occurs check: cannot construct the infinite type: t0 = [t0]
    In the first argument of `(:)', namely `(y)'

19 refers to line 2, the first split2 function. Not exactly sure how to go about fixing this error. Any ideas?

  • 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-12T18:31:31+00:00Added an answer on June 12, 2026 at 6:31 pm

    It’s hard to know where to start…

    Let’s define functions from ever larger chunks of the expression in split2.

    f1 (x:y:xs) = (length(x:y:xs) `div` 2)-2
    f1 :: [a] -> Int
    

    Ok, so the argument is a list of something, and it returns an Int

    f2 (x:y:xs) = ((length(x:y:xs) `div` 2)-2) : x
    f2 :: [[Int]] -> [Int]
    

    Here, the length Int is being cons’d with x, so x must be [Int], so (x:y:xs) must be [[Int]]. We can also infer that y has the same type as x, and xs is a list of things of the same type; [[Int]]. So the x ++ y will also be [Int].

    So, [xs] will have type [[[Int]]]. Now, we wrap the result in a list constructor, and cons it with [xs]:

    f3 (x:y:xs) = [((length(x:y:xs) `div` 2)-2) : x ++ y] : [xs]
    f3 :: [[Int]] -> [[[Int]]]
    

    I’m guessing you didn’t expect the argument to be a list of lists of lists of Ints.

    Now, if we look at split2, the argument pattern (x:xs:[y:ys]) implies that it is of type:

    split2 :: [[a]] -> b
         x :: [a]
        xs :: [a]
         y ::  a
        ys :: [a]
    

    The rhs of the first definition of split2 tries to construct a new list by concatenating (x-1) : [xs] and y : [ys]. However, if we substitute the types into the y : [ys], we find:

    y : [ys] :: a : [[a]]
    

    But since (:) :: a -> [a] -> [a], this means that [[a]] must be the same type as [a], or a must be a list of itself, which is not possible.

    The (x-1) is also badly typed, because it attempts to subtract one from a list.

    I can’t tell if you want to split the lists into even and odd elements, or into first and second halves.

    Here are two versions that split into the first and second halves, rounding down (RD) or up (RU) if the length is odd:

    splitRD xs = splitAt (length xs `div` 2) xs
    splitRU xs = splitAt ((length xs + 1) `div` 2) xs
    

    Here’s a version that splits the list into even and odd elements:

    splitEO [] = ([], [])
    splitEO [e] = ([e], [])
    splitEO (e:o:xs) = (e:es, o:os) where (es, os) = splitEO xs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm practicing Haskell, and writing a summation function that takes in two numbers (upper
I have a recursive function working on a list, the function contains a loop
I'm working on a function that is too complicated (processor) so I embedded part
I'm working on a function in MATLAB that reads input from a file. So
I am working on a function that accepts a JSON object as a parameter.
I'm working on a function that handles events from a number of buttons and
I implemented a binary to decimal function in Haskell and am currently working on
I'm working through Real World Haskell one of the exercises of chapter 4 is
I'm trying to make a function that does the following: it gets a list
I'm teaching myself to program in Haskell, and I'm working on a find function.

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.