Question: Write two functions in SML : even and odd. even will test if the argument is even, and odd will test if the argument is

Write two functions in SML: even and odd. even will test if the argument is even, and odd will test if the argument is odd.

For the infinite lists, create one list called allZeros that contains only the integer 0. Create another infinite list called allOnes that contains only the integer 1. Create a third infinite list called evens that has only the even numbers in the set of positive integers, including zero. Create yet another infinite list called odds that has only the odd numbers in the set of positive integers. You should use the FILTER, FROMN, even, and odd to create evens and odds by filtering the even and odd positive integers (including zero), respectively. The last two infinite lists are called fibs and has the entire Fibonacci sequence starting from zero, and primes which contains set of prime numbers.

Below are the datatype and function bindings required to be used.

datatype 'a inflist = NIL

| CONS of 'a * (unit -> 'a inflist);

exception Empty;

exception Subscript;

fun HD (CONS(a,b)) = a

| HD NIL = raise Empty;

fun TL (CONS(a,b)) = b()

| TL NIL = raise Empty;

fun NUL NIL = true

| NUL _ = false;

fun NTH 0 L = HD L

| NTH n L = NTH (n-1) (TL L);

fun TAKE (xs, 0) = []

| TAKE (NIL, n) = raise Subscript

| TAKE (CONS(x, xf), n) = x::TAKE(xf(), n-1);

fun FROMN n = CONS(n, fn () => FROMN (n+1));

fun FIB n m = CONS(n, fn () => FIB m (n+m));

fun FILTER f l =

if NUL l

then NIL

else if f (HD l)

then CONS(HD l, fn() => (FILTER f (TL l)))

else FILTER f (TL l);

fun SIFT NIL = NIL

| SIFT l =

let val a = HD l

in CONS(a, fn () => SIFT(FILTER (fn x => x mod a <> 0) (TL l)))

end;

(**********************

*

* FUNCTION AND INFLIST STUBS -- YOU MUST IMPLEMENT THESE

*

**********************)

fun even (x : int) : bool = true

fun odd (x : int) : bool = true

val fibs = NIL

val evens = NIL

val odds = NIL

val allZeros = NIL

val allOnes = NIL

val primes = NIL

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!