Week 3
Quiz 9

Quiz 9: Using either

Question 1

Shows cannot divide by 0?
JavaScript
// standard either
const Left = x => f => g => f (x);
const Right = x => f => g => g (x);
const either = e => f => g => e (f) (g);
 
const saveDiv = num => divisor =>
    divisor === 0
        ? Left("cannot divide by 0")
        : Right(num / divisor);
 
const result = saveDiv(1)(0);
 
either(result)
    (msg => document.writeln(msg))
    (val => document.writeln(val === 1));

Question 2

Shows true?
JavaScript
// standard either
const Left = x => f => g => f (x);
const Right = x => f => g => g (x);
const either = e => f => g => e (f) (g);
 
const saveDiv = num => divisor =>
    divisor === 0
        ? Left("cannot divide by 0")
        : Right(num / divisor);
 
const result = saveDiv(1)(1);
 
either(result)
    (msg => document.writeln(msg))
    (val => document.writeln(val === 1));

Question 3

Shows cannot divide by 0?
JavaScript
// standard either
const Left = x => f => g => f (x);
const Right = x => f => g => g (x);
const either = e => f => g => e (f) (g);
 
const saveDiv = num => divisor =>
    divisor === 0
        ? Left("cannot divide by 0")
        : Right(num / divisor);
 
const result = saveDiv(1)(0);
 
either(result)
    (msg => document.writeln(msg));

Question 4

Fails with error message?
JavaScript
// standard either
const Left = x => f => g => f (x);
const Right = x => f => g => g (x);
const either = e => f => g => e (f) (g);
 
const saveDiv = num => divisor =>
    divisor === 0
        ? Left("cannot divide by 0")
        : Right(num / divisor);
 
const result = saveDiv(1)(0);
 
either(result)
    (msg => document.writeln(msg));

Question 5

Produces no output (fails silently)?
JavaScript
// standard either
const Left = x => f => g => f (x);
const Right = x => f => g => g (x);
const either = e => f => g => e (f) (g);
 
const saveDiv = num => divisor =>
    divisor === 0
        ? Left("cannot divide by 0")
        : Right(num / divisor);
 
const result = saveDiv(1)(0);
 
either(result)
    (msg => document.writeln(msg));