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
inx
, wherex
represents a variable if (x) { }
- which executes any code within
{ }
ifx
istrue
(or any value other than0
) print(x)
- which prints
x
(which can be of any type) add(x, y)
- which returns
x
plusy
, which are assumed to be integers subtract(x, y)
- which returns
x
minusy
, which are assumed to be integers multiply(x, y)
- which returns
x
timesy
, which are assumed to be integers divide(x, y)
- which returns
x
divided byy
, which are assumed to be integers, using integer division (and truncating) like C greater(x, y)
- which returns
true
ifx
is greater thany
orfalse
ifx
is not greater thany
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)
-
(3 points.) In creating B, it seems Brian forgot to add support for the boolean “or” operator (which is
||
in C). Complete the implementation ofor
, below, which accepts two Boolean values,$a
and$b
, as input and returnstrue
if either or both of$a
and$b
aretrue
, otherwise returningfalse
, using only those building blocks that Brian did implement.function or($a, $b) { // TODO }
-
(3 points.) In creating B, it seems Brian forgot to add support for the boolean “and” operator (which is
&&
in C). Complete the implementation ofand
, below, which accepts two Boolean values,$a
and$b
, as input and returnstrue
if both$a
and$b
aretrue
, otherwise returningfalse
, using only those building blocks that Brian did implement.function and($a, $b) { // TODO }
-
(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 be2
). Complete the implementation ofremainder
, 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 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 }