Type definitions: Write down two valid sets of type definitions, one in Java not
ID: 3839415 • Letter: T
Question
Type definitions:
Write down two valid sets of type definitions, one in Java notation
and one in Haskell notation, that could reasonably be used to describe
the type of X for the following description:
X is a predicate on integer values. (i.e., X can be used as a
function mapping integers to Booleans.) For example, X could be the
predicate for testing whether a given number is even, or for testing
whether it is prime, and so on. Note, however, that you are only
expected to specify the *type* of X, and should NOT provide any specific
values of that type.
For example, if "X is a description of a person, capturing their name,
date of birth, and social security number", then you might offer the
following pair of data type definitions, in each case suggesting that X
could be represented by a value of type Person:
Java Notation: | Haskell Notation:
---------------------+--------------------------------------
class Person { | data Person = MkPerson String Date Int
String name; |
Date dob; |
int ssn; |
} |
|
class Date { | data Date = MkDate Int Int Int
int month; |
int day; |
int year; |
} |
Explanation / Answer
Java Notation: There is a class called Predicate that can be used to perform predicates o any type. So for Integers it would be
Predicate<Integer>
Example:
public static Predicate<Integer> CheckNumber() {
return p -> p.valueOf(5) > 2;
}
Haskell Notation:
eqInteger :: Integer -> Integer -> Bool
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.