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
namethat accepts as input a comma-separated list of zero or more parameters return(value)- which returns a single
valuefrom a function $variable- which represents a variable (of any type), whose name must start with a dollar sign, called
$variable x <- y- which stores
yinx, wherexrepresents a variable if (x) { }- which executes any code within
{ }ifxistrue(or any value other than0) print(x)- which prints
x(which can be of any type) add(x, y)- which returns
xplusy, which are assumed to be integers subtract(x, y)- which returns
xminusy, which are assumed to be integers multiply(x, y)- which returns
xtimesy, which are assumed to be integers divide(x, y)- which returns
xdivided byy, which are assumed to be integers, using integer division (and truncating) like C greater(x, y)- which returns
trueifxis greater thanyorfalseifxis 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,$aand$b, as input and returnstrueif either or both of$aand$baretrue, 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,$aand$b, as input and returnstrueif both$aand$baretrue, 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 % 3would be2). Complete the implementation ofremainder, below, which accepts two integers,$aand$b, as input and returns the remainder when dividing$aby$b, using only those building blocks that Brian did implement. Assume that both$aand$bwill 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$ntimes, using only those building blocks that Brian did implement. No need to include any\n. Assume that$nwill be a non-negative integer.function meow($n) { // TODO }