144k views
3 votes
Write a type definition appropriate for the Set type. You can try something like this: data Set a=… or like this: type set a=… Write a function setSuchThat :: (a → Bool)-> Set a that will create a new set from a given characteristic function. Write a function setUnion : : Set a → Set a → Set a that will create a new set on the basis of the union of two existing sets.

User Mrydengren
by
8.7k points

1 Answer

2 votes

Final answer:

To define a Set type in Haskell, you can use the 'data' keyword followed by the type name and the type parameters. The 'setSuchThat' function can create a new set from a given characteristic function. The 'setUnion' function can create a new set based on the union of two existing sets.

Step-by-step explanation:

To define a Set type in Haskell, you can use the 'data' keyword followed by the type name and the type parameters. Here's an example:

data Set a = Set [a]

This defines a Set type that takes a type parameter 'a' and has a single constructor 'Set' which takes a list as its argument. The 'setSuchThat' function can be defined as:

setSuchThat :: (a -> Bool) -> Set a
setSuchThat f = Set (filter f xs)

Here, the function takes a predicate function 'f' and a Set 'xs', and returns a new Set that contains the elements from 'xs' for which 'f' returns 'True'. The 'setUnion' function can be defined as:

setUnion :: Set a -> Set a -> Set a
setUnion (Set xs) (Set ys) = Set (xs ++ ys)

This function takes two Sets 'xs' and 'ys', and returns a new Set that contains all the elements from 'xs' followed by all the elements from 'ys'.

User Groteworld
by
9.0k points