1

I am currently programming in BCPL for an OS course and wanted to write a simple is_digit() function for validation in a program of mine.

A code snippet of my current code follows:

let is_digit(n) be {
  if ((n >= '0') /\ (n <= '9')) then 
    resultis true;
}

I am aware that BCPL has no notion of types, but how would I be able to accomplish this sort of thing in the language?

Passing in a number yields a false result instead of the expected true.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
User 5842
  • 2,849
  • 7
  • 33
  • 51

2 Answers2

2

is_digit() is a function returning a value, rather than a routine, so should use = VALOF rather than BE. Otherwise, the code is OK.

let is_digit(n) = valof {
    .....
    resultis true
 }
0

Functions that return values should be using valof rather than be, the latter (a routine rather than a function) can be called as a function but the return value you get back from it will be undefined(a).

In addition, you should ensure you return a valid value for every code path. At the moment, a non-digit will not execute a RESULTIS statement, and I'm not entirely certain what happens in that case (so best to be safe).

That means something like this is what you're after, keeping in mind there can be implementation variations, such as & and /\ for and, or {...} and $(...$) for the block delimiters - I've used the ones documented in Martin's latest manual:

LET is_digit(n) = VALOF {
    RESULTIS (n >= '0') & (n <= '9')
}

(a) Since Martin Richards is still doing stuff with BCPL, this manual may help in any future questions (or see his home page for a large selection of goodies).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953