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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:15:40+00:00 2026-06-12T15:15:40+00:00

I am writing a Haskell function that takes a list of strings and returns

  • 0

I am writing a Haskell function that takes a list of strings and returns a list containing the first two strings as a tuple as the result. So an example output would be:

listtuple ["bride", "zilla", "crazy", "women"] = [("bride", "villa")]
listtuple ["basketball", "football"] = [("basketball", "football")]

The way I was thinking of approaching it like so:

listtuple :: Eq a => [Str a] -> [(Str a, Str a)]
listtuple xs = [(x,y) | x <- xs !! 0, y <- xs !! 1]

Essentially I figured that I could just just pick the elements in the first and second indices of the list but I’m getting errors. Any help here?

  • 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-12T15:15:41+00:00Added an answer on June 12, 2026 at 3:15 pm

    What you probably wanted to do was this:

    listtuple xs =
        let x = xs !! 0
            y = xs !! 1
         in (x, y)
    

    Note that this is NOT the same thing as what you wrote. The reason is that the list comprehension you wrote translates into the following:

    do x <- xs !! 0  -- Treat the first  element of xs as a list
       y <- xs !! 1  -- Treat the second element of xs as a list
       return (x, y)
    

    This illustrates the issue: You are treating xs !! 0 and xs !! 1 as lists when they are not lists. xs !! 0 is just a single element, so if you want to declare that x is equal to xs !! 0, you use:

    let x = xs !! 0
     in <some expression that uses x>
    

    The <- in list comprehension syntax does not do the same thing and I recommend you steer clear of list comprehensions until you understand how the list monad works, because the compiler translates list comprehensions to the list monad.

    Now, the second problem is that you use (!!). You should steer clear of partial functions like (!!) and focus on using pattern matching to solve these things. The idiomatic way to do what you request would be to pattern match on the first two elements:

    listtuple (x:y:_) = (x, y)
    

    … EXCEPT that will fail on lists that contain less than two elements. You guard against this by storing the result as a Maybe, where a Just wraps a successful result, and Nothing indicates failure:

    listtuple :: [a] -> Maybe (a, a)
    listtuple (x:y:_) = Just (x, y)
    listtuple _       = Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a function in Haskell that recursively returns the list with the specified
I'm practicing Haskell, and writing a summation function that takes in two numbers (upper
How would you go about defining a function that takes two strings, say string
I am writing a function in Haskell that deals with numbers beyond the length
Using Haskell, I'm writing a function that counts the total number of leaves in
I've got a function that takes data and either returns the same data or
I'm writing an audio program in Haskell using Portaudio. I have a function that
I am new to Haskell,I should write a function that takes a function among
Today I am writing a small program in Haskell. I found that in ghci's
recently I'm writing some functions that I take from Haskell and translate into Java.

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.