Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp2023:lab04 [2023/03/21 07:23] pdmatei |
fp2023:lab04 [2023/03/24 14:53] (current) pdmatei |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Lab 4. Algebraic Datatype definition ====== | ====== Lab 4. Algebraic Datatype definition ====== | ||
- | |||
- | ==== 4.1. The type Nat ===== | ||
- | Consider the following type defined to represent natural numbers: | ||
- | <code scala> | ||
- | trait Nat | ||
- | case object Zero extends Nat | ||
- | case class Succ(n: Nat) extends Nat | ||
- | </code> | ||
- | |||
- | **4.1.1.** Implement the following function: | ||
- | <code scala> | ||
- | def isZero(n: Nat): Nat = | ||
- | n match { | ||
- | case Zero => ??? | ||
- | case Succ(np) => ??? | ||
- | } | ||
- | </code> | ||
- | |||
- | **4.1.2.** Implement the addition function over natural numbers: | ||
- | <code scala> | ||
- | def add(n: Nat, m: Nat): Nat = | ||
- | n match { | ||
- | case Zero => ??? | ||
- | case Succ(np) => ??? | ||
- | } | ||
- | </code> | ||
- | |||
- | **4.1.3.** Implement the subtraction function over natural numbers. If $math[n > m] then $math[m - n = 0]. | ||
- | <code scala> | ||
- | def subtract(n: Nat, m: Nat): Nat = ??? | ||
- | </code> | ||
- | |||
- | **4.1.4.** Implement ''greater'' which checks if a natural number is strictly larger than the other: | ||
- | <code scala> | ||
- | def greater(n: Nat, m: Nat): Nat = | ||
- | (n,m) match { | ||
- | ??? | ||
- | } | ||
- | </code> | ||
- | |||
- | **4.1.5.** Equality is already defined for all case classes, as structural equality (objects built in the same way are equal). However, re-implement equality over naturals: | ||
- | <code scala> | ||
- | def equal(n: Nat, m: Nat): Nat = ??? | ||
- | </code> | ||
- | |||
- | **4.1.6.** Implement a function which converts a Nat to a Scala Int: | ||
- | <code scala> | ||
- | def toInt(n: Nat): Int = ??? | ||
- | </code> | ||
- | |||
- | **4.1.7.** Implement a function which converts an Int to a Nat: | ||
- | <code scala> | ||
- | def fromInt(i: Int): Nat = ??? | ||
- | </code> | ||
- | ===== 4.2. The type IList (list over integers) ===== | ||
Consider the following type defined to represent lists of integers: | Consider the following type defined to represent lists of integers: | ||
Line 63: | Line 8: | ||
</code> | </code> | ||
- | **4.2.1.** Implement ''isEmpty'' which checks if a list is empty: | + | **4.1.** Implement ''isEmpty'' which checks if a list is empty: |
<code scala> | <code scala> | ||
def isEmpty(l: IList): Boolean = ??? | def isEmpty(l: IList): Boolean = ??? | ||
</code> | </code> | ||
- | **4.2.2.** Implement ''size'' which returns the size of the list: | + | **4.2.** Implement ''size'' which returns the size of the list: |
<code scala> | <code scala> | ||
def size(l: IList): Int = ??? | def size(l: IList): Int = ??? | ||
</code> | </code> | ||
- | **4.2.3.** Implement ''append'' which concatenates two lists: | + | **4.3.** Implement ''append'' which concatenates two lists: |
<code scala> | <code scala> | ||
def append(l1: IList, l2: IList): IList = ??? | def append(l1: IList, l2: IList): IList = ??? | ||
</code> | </code> | ||
- | **4.2.4.** (!) Implement ''last'' which returns the last element from a list: | + | **4.4.** (!) Implement ''last'' which returns the last element from a list: |
<code scala> | <code scala> | ||
def last(l: IList): Int = ??? | def last(l: IList): Int = ??? | ||
</code> | </code> | ||
- | **4.2.5.** (!) Implement ''reverse''. There are two different ways to implement reverse (with direct and with tail-end recursion). Try both implementations. | + | **4.5.** (!) Implement ''reverse''. There are two different ways to implement reverse (with direct and with tail-end recursion). Try both implementations. |
<code scala> | <code scala> | ||
def reverse(l: IList): IList = ??? | def reverse(l: IList): IList = ??? | ||
</code> | </code> | ||
- | **4.2.6.** Implement ''contains'' which checks if an element is a member of a list. | + | **4.6.** Implement ''contains'' which checks if an element is a member of a list. |
+ | <code scala> | ||
+ | def contains(e: Int, l: IList): Boolean = ??? | ||
+ | </code> | ||
+ | |||
+ | **4.7.** Implement ''max'' which returns the largest integer from a list: | ||
+ | <code scala> | ||
+ | def max(l: IList): Int = ??? | ||
+ | </code> | ||
+ | |||
+ | **4.8.** Implement ''take'' which returns a new list containing the first ''n'' elements of the original list: | ||
<code scala> | <code scala> | ||
- | def contains(e: Int, l: IList): IList = ??? | + | def take(n: Int)(l: IList): IList = ??? |
</code> | </code> | ||
- | **4.2.7.** Implement ''max'' which returns the largest integer from a list: | + | **4.9.** Implement ''drop'' which returns a new list containing the original list without the first ''n'' elements: |
<code scala> | <code scala> | ||
- | def max(l: Ilist): IList = ??? | + | def drop(n: Int)(l: IList): IList = ??? |
</code> | </code> | ||
- | **4.2.8.** Implement ''isSorted'' which checks if a list is sorted: | + | **4.10.** Implement ''isSorted'' which checks if a list is sorted: |
<code scala> | <code scala> | ||
- | def isSorted(l: IList): IList = ??? | + | def isSorted(l: IList): Boolean = ??? |
</code> | </code> | ||
- | **4.2.9.** Implement ''merge'' which merges two sorted lists: | + | **4.11.** Implement ''merge'' which merges two sorted lists: |
<code scala> | <code scala> | ||
def merge(l1: IList, l2: IList): IList = ??? | def merge(l1: IList, l2: IList): IList = ??? | ||
</code> | </code> | ||
- | **4.2.10.** Implement ''mergeSort'' which sorts a list: | + | **4.12.** Implement ''mergeSort'' which sorts a list: |
<code scala> | <code scala> | ||
def mergesort(l: IList) IList = ??? | def mergesort(l: IList) IList = ??? | ||
</code> | </code> |