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

  • Home
  • SEARCH
  • 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 9087103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:38:23+00:00 2026-06-16T21:38:23+00:00

Disclaimer: this is a part of a homework assignment. I want to implement the

  • 0

Disclaimer: this is a part of a homework assignment.
I want to implement the flatMap for the custom List object. I have successfully implemented map, but I have problem with flatMap. I do not know how to flatten the List of Lists that I get from map. I do not know if I really should use map at all.

trait List[+A] {
  /** The first element */
  def head: A
  /** The rest of the elements */
  def tail: List[A]
  def flatMap[B](f: A => List[B]): List[B]
  def map[B](f: A => B): List[B]

  // Concatenate two lists
  def concat[B >: A](that: List[B]): List[B] = this match {
    case Empty => that
    case NonEmpty(head, tail) => NonEmpty(head, tail concat that)
  }
}

case object Empty extends List[Nothing] {
  def head = throw new UnsupportedOperationException("Empty.head")
  def tail = throw new UnsupportedOperationException("Empty.tail")
  def flatMap[B](f: Nothing => List[B]): List[B] = Empty
  def map[B](f: Nothing => B): List[B] = Empty

  override def toString = "Empty"
}

case class NonEmpty[A](head: A, tail: List[A]) extends List[A] {

  def map[B](f: A => B): List[B] = {

    NonEmpty(f(head), tail.map(f))

  }
def flatMap[B](f: A => List[B]): List[B] = {
    val a = this.map(f)
    for (x <- a; y <- x) yield y
  }
}
  • 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-16T21:38:25+00:00Added an answer on June 16, 2026 at 9:38 pm

    You have to write flatMap for a list with length n. Try to solve it supposing that you have already solved it for a list with length n-1. If you can do this, then you solved the problem, because n => n-1 => … => 1 => 0, and for 0 you already has a solution.

    This kind of thinking is suitable for your List, because it is a recursive type.

    You did this already with map, do the same with flatMap. Both function is a transformation from List[A] to List[B], the only difference is the tool what they can use, map has a function that converts A to B, while flatMap has a function that converts A to List[B]

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

Sidebar

Related Questions

Disclaimer: this question is directly related to my programming homework. My C++ assignment consists
Disclaimer: this is homework. I have a SAS dataset of: ID VAL YEAR I
Disclaimer: This is homework. I am attempting it and do not expect or want
Disclaimer: This is for a homework assignment, but the question is not regarding the
(Disclaimer: I realize this is a massive wall of text, but I have done
Disclaimer: This is for an assignment. I am not asking for explicit code answers,
Disclaimer: This is part of the requirement of the program, so it's not meant
Disclaimer: i have searched genericly (Google) and here for some information on this topic,
Disclaimer: this is a (frustrating) homework related problem. I'm having odd results when I
Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.

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.