Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package functional import functional.data. { Graph , Tree } trait Foldable [ F [ _ ] ] : def fold Monoid [ A ] def

package functional
import functional.data.{Graph, Tree}
trait Foldable[F[_]]:
def fold Monoid [A]
def foldMap Monoid [B] : B
object ListFoldable extends Foldable[List]:
override def fold : List [A](m: Monoid [A] : A=t match
case Nil => m.empty
case , fold (tl)(m)
override def foldMap List [A] Monoid [B]???
object TreeFoldable // TODO
|
object GraphFoldable // TODO
def wordCount[F[_]](t: F[String])(f: Foldable[F]): Int =???
def charCount[F[_]](t: F[String])(f: Foldable[F]): Int =???
2a) Implement foldables (5 points)
The foldables for the data structures List, Tree and Graph need to be implemented.
Important: The given interfaces do not use any implicit parameters. You need to find the appropriate monoids explicitly
passed as a parameter.
Hints:
Two graphs can be merged using the monoid GraphMerge (see task 1).
The neighboring nodes of a graph can be edited using ListFoldable.
2b) Apply foldables (2 points)
Task: Implement the two methods wordCount and charCount in functional.Foldable. The method
Example: wordCount(List("hello world", "foo bar"))(ListFoldable)==4
charCount should return the total number of characters in the strings contained within the foldable data structure.
Example: charCount(List("hello world", "foo bar"))(ListFoldable)==18
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions