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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:05:42+00:00 2026-06-12T23:05:42+00:00

I am trying to wrap my head around the syntax of Haskell. This problem

  • 0

I am trying to wrap my head around the syntax of Haskell.

This problem is very simple to solve logically. I have to break up a list of positive and negative integers and group them such that

[1,2,3,-1,-2,-3,1,2,3] becomes [[1,2,3],[-1,-2,-3], [1,2,3]]

I would like to use a higher order function, foldr to be able to do that with an anonymous function taking in two arguements.

This is what I have so far.

split = foldr (\ x y -> if (x > 0) 
                        then if (head (head y)) < 0
                            then [x] : y
                            else x : head y --error here
                        else if (x < 0) 
                        then if (head (head y)) > 0
                            then [x] : y
                            else x : head y
                        else y
                )
                [[]]

this is the error i get

 Occurs check: cannot construct the infinite type: a0 = [a0]
    In the first argument of `(:)', namely `x'
    In the expression: x : head y
    In the expression:
      if (head (head y)) < 0 then [x] : y else x : head y

I have two questions.

1) Why am I getting a type error at line 7?

Am I not concatenation an integer (x) to a list of integers (head y)

2) How do you write the conditions out using guards? I tried doing it but I kept getting parsing error at '|'

  • 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-12T23:05:42+00:00Added an answer on June 12, 2026 at 11:05 pm

    You’re simply missing to keep tail y. In

    foldr (\ x y -> if (x > 0) 
                        then if (head (head y)) < 0
                            then [x] : y
                            else x : head y
    

    you have x :: (Num a, Ord a) => a, y :: (Num a, Ord a) => [[a]], and head y :: (Num a, Ord a) => [a].

    So forgetting the tail y shaves off one layer of []. the else branch should be

    else (x:head y) : tail y
    

    in both branches of the outer if.

    But, your function has two semantic problems after that.

    First, you don’t treat the case that head y is empty, that will cause an exception when the end of the list is reached, and second, it doesn’t work on infinite lists, since the combinator function doesn’t construct anything of the result before its second argument is known. If the latter is a problem, you can find a sufficiently lazy combinator function in this answer.

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

Sidebar

Related Questions

I'm trying to wrap my head around linq -> nhib I have a simple
Trying to wrap my head around JTA and have arbitrarily chosen Bitronix as the
I'm trying to wrap my head around this and I think the easiest way
I'm trying to wrap my head around the proper syntax to achieve the following--or
I am trying to wrap my head around why we first approach the problem
Trying to wrap my head around the apple design scheme. I have a UIViewController
I'm trying to wrap my head around this new concept called LINQ. Ever heard
I'm trying to wrap my head around this to make the correct design decisions.
I'm trying to wrap my head around this, but I seem to go in
I'm trying to wrap my head around multiple inheritance in python. Suppose I have

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.