Programming in B

Suppose that Brian has designed his own programming language called B. The language is “loosely typed,” which means you needn’t specify a variable’s type: values that look like integers (e.g., -1, 0, 1, et al.) will be treated as integers, values that look like strings will be treated as strings, and true and false will be treated as Boolean values. As in C, comments in B begin with //. But the language comprises only these building blocks, wherein { } represents one or more lines of code:

function name(parameters) { }
which defines a function called name that accepts as input a comma-separated list of zero or more parameters
return(value)
which returns a single value from a function
$variable
which represents a variable (of any type), whose name must start with a dollar sign, called $variable
x <- y
which stores y in x, where x represents a variable
if (x) { }
which executes any code within { } if x is true (or any value other than 0)
print(x)
which prints x (which can be of any type)
add(x, y)
which returns x plus y, which are assumed to be integers
subtract(x, y)
which returns x minus y, which are assumed to be integers
multiply(x, y)
which returns x times y, which are assumed to be integers
divide(x, y)
which returns x divided by y, which are assumed to be integers, using integer division (and truncating) like C
greater(x, y)
which returns true if x is greater than y or false if x is not greater than y

As in C, function calls can be nested: one function’s output can be passed to another function as input. But no need for semicolons!

B has no other features. In particular, it does not support binary operators like +, -, *, /, %, &&, ||, !, >, >=, ==, or <=, though it does support - as a prefix for negative integers. But you can still program in it!

For instance, whereas in C you might write:

int c = a - b;

in B you would write:

$c <- subtract($a, $b)
  1. (3 points.) In creating B, it seems Brian forgot to add support for the boolean “or” operator (which is || in C). Complete the implementation of or, below, which accepts two Boolean values, $a and $b, as input and returns true if either or both of $a and $b are true, otherwise returning false, using only those building blocks that Brian did implement.

    function or($a, $b)
    {
        // TODO
    }
    
  2. (3 points.) In creating B, it seems Brian forgot to add support for the boolean “and” operator (which is && in C). Complete the implementation of and, below, which accepts two Boolean values, $a and $b, as input and returns true if both $a and $b are true, otherwise returning false, using only those building blocks that Brian did implement.

    function and($a, $b)
    {
        // TODO
    }
    
  3. (3 points.) In creating B, it seems Brian forgot to add support for the remainder operator (which is % in C), which computes the remainder when one integer is divided by another. (For example, 8 % 3 would be 2). Complete the implementation of remainder, below, which accepts two integers, $a and $b, as input and returns the remainder when dividing $a by $b, using only those building blocks that Brian did implement. Assume that both $a and $b will be positive integers.

    function remainder($a, $b)
    {
        // TODO
    }
    
  4. (4 points.) In creating B, it seems Brian forgot to add support for loops. Nonetheless, complete the implementation of meow, below, which should print "meow" a total of $n times, using only those building blocks that Brian did implement. No need to include any \n. Assume that $n will be a non-negative integer.

    function meow($n)
    {
        // TODO
    }