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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:42:59+00:00 2026-06-05T09:42:59+00:00

It is a part of a homework. I need to write a function that

  • 0

It is a part of a homework.
I need to write a function that reads info from the input until empty line. After that function puts first, third, fifth … line symbols as one String and second, fourth … as another String.
Signature is combine :: IO (String , String)

I have written a function that takes a list as argument and puts 1,3,5.. to one String a 2,4,6 symbols to another String. Function is here:

intotwo (x : xs)
   = let
      (us , vs)
         = intotwo xs
   in
   (x : vs , us)
intotwo _
   = ([] , []) 

Also I have written a code that reads an input: It is here:

combine
   = do
      lines <- getLine
      if null lines
         then return ([], [])
         else do
            linesagain <- combine
            return --what should I return?

Can anybody help me to finish my homework (optional: give some tips)?

  • 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-05T09:43:00+00:00Added an answer on June 5, 2026 at 9:43 am

    I’ll give a few hints to help you solve your problem.

    Firstly, it is a HUGE help to give types to all the functions you define. This lets the compiler tell you immediately if a function isn’t doing what you think it is doing. It also helps you when you need to join different parts of your program together.

    Secondly, it is a very good idea to only solve one problem per function. Your intotwo function follows this well, it does its job of splitting a list very well. Your combine function however looks like it is trying to do too much, which will make it harder to write. I would split that function into two smaller functions.

    Finally, there is a very useful special function called undefined. This matches any type, and helps you write a function. The trick is to start with the whole function equalling undefined (and compiling but crashing when run), and progressively improve the function until it does what you want and has no more undefines in it.

    So firstly, I would add a type signature to the intotwo function. THe type is [a] -> ([a], [a]).

    Next, I would write a function that reads lines from input until it hits an empty line, then returns the list of lines read. This function would have the type:

    readLinesUntilEmpty :: IO [String]
    readLinesUntilEmpty = undefined
    

    An implementation that nearly does this is this:

    readLinesUntilEmpty = do
      nextLine <- getLine
      rest <- readLinesUntilEmpty
      return (nextLine : rest)
    

    However this never stops reading (note how nextLine isn’t checked for being empty).

    the following function shows how you can do this (but I’m leaving some of the implementation out):

    readLinesUntilEmpty = do
      nextLine <- getLine
      case nextLine of
        "" -> do
          undefined -- TODO fix me
        _ -> do
          undefined -- TODO fix me
    

    You should be able to figure out the rest from there.

    Next, your intotwo function returns two lists of strings, but you are expected to join them together again. Eg ["this", "that"] should turn into "this\nthat". The type of such a function is [String] -> String:

    joinLines :: [String] -> String
    joinLines = undefined -- TODO
    

    This is an easier function to write than the inttotwo func, so you should be fine.

    Finally, you can link those three functions together to get your result:

    combine :: IO (String, String)
    combine = do
      lines <- readLinesUntilEmpty
      let (oddLines, evenLines) = intotwo lines
      return $ (joinLines oddLines, joinLines evenLines)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of a homework assignment I need to concatenate certain values in an
I made a swing calculator as part of homework that I did and would
so this is part of my homework assignment. need some help with it. my
I've been asked (as part of homework) to design a Java program that does
As part of a homework assignment, we are supposed to create an array that
As part of a homework assignment, I have to write a C program in
This is part of a homework assignment. What we have to do is write
I need to implement a digraph(Directed graph) in c++ as part of a homework
I am doing homework and I got to the part where I need to
As part of my homework I have the following code: ArrayList <Integer> marks =

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.