Join the M revolution – M and R programming languages

No readers like this yet.
MUMPS, M and R programming languages

Opensource.com

“'I feel and think much as you do, care about many of the things you care about, although most people do not care about them. You are not alone.” Kurt Vonnegut

Previous articles

Developers who take a first peek at the M language may get a quick impression that it is strange and alien. Here is the good news: If you have used R, or have friends who know R, then you are in good company and can learn M in a much shorter time. Moreover, you can combine M with R to get a powerful with an excellent statistical package.

All is good and well in the open source universe.

Enough talking, let's start typing.

Open two terminals. On the left is your M interpreter, and on your right is your R interpreter.

 

M

R

Start the interpreter

gtm

R

The prompt you will see

GTM>

>

How to quit the interpreter

HALT

q()

Ask for general help

ZHELP

help()

Ask for specific help

ZHELP("HALT")

help("q")

Assigning a variable

SET A=10

SET X="Higgs"

A <- 10

X <- "Higgs"

Displaying a variable

WRITE A

print(A)

Variables are case sensitive

SET A=12

SET a=15

WRITE A

WRITE a

A <-12

a<-15

print(a)

print(A)

Lists assignment

SET k("name")="Mary", k("spouse")="John", k("father")="Peter", k("mother")="Anne"

k <- list(name="Mary", spouse="John", father="Peter", mother="Anne")

List element access

WRITE k("name")

WRITE k("spouse")

WRITE k("father")

WRITE k("mother")

SET idx="name"

WRITE k(idx)

print(k[["name"]])

print(k[["spouse"]])

print(k[["father"]])

print(k[["mother"]])

idx <- "name"

print(k[[idx]])

Basic functions

SQR(x)

QUIT x*x

sqr < - function(x) {

x*x }

Calling function

WRITE $$SQR^SQR(3)

print(sqr(3))

The code is alive

R has a built-in object-oriented model, while M has a flexible data model. Both of them enable you to express situations such as: An object has a set of data properties, and a set of behaviors, and both of them can be changed at run time.

For example, let's think of a car. Let's consider three basic properties of a car: make, model, and mileage. Let's assume that the make and model are kinds of “data” properties of the car, while the mileage is something to be computed out of experimental conditions, and that is probably changing with the way you drive the car.

Let's express this in R:

car <- list(make=”toyota”,model=”corolla”,mileage=5)

Now, given the flexibility that late-binding gives us in R, we can decide to change mileage from a number into a function:

car$mileage <- function(miles,gallons) { miles/gallons }

Finally, we can query information from the car:

            print(car$make)

            print(car$model)

            print(car$mileage(150,4))

Now let's do the equivalent in M:

            SET ^car(“make”)=”toyota”,^car(“model”)=”corolla”,^car(“mileage”)=5

Then, use the ZEDIT command to put the following code in a file called mileage.m:

            ZEDIT “mileage”

Then type:

            compute(miles,gallons)

             quit miles/gallons

Note that in the second line, the “quit” command is indented one space to the right. Save the file and go back to the interpreter. Then type:

            SET ^car(“mileage”)=”$$compute^mileage”

Finally, we query the information from the car:

            WRITE ^car(“make”)

            WRITE ^car(“model”)

            WRITE @^car(“mileage”)@(150,4))

Note that the “@” symbols surrounding the string “^car(“mileage”)” indicate that there is a level of indirection to be done by the interpreter. In other words, they tell the interpreter to look at the content of the field “mileage” in the array “^car” and replace it in this expression.

These examples illustrate that M--just like R--provides a context in which one can compose complex constructs and use them to model concepts of real world problems.

Now imagine: What happens when we combine one of the most powerful statistical open source languages with one of the most powerful NoSQL open source databases? 

We’ll talk about this next time.

Learn more

User profile image.
Luis Ibáñez works as Senior Software Engineer at Google Inc in Chicago.

5 Comments

Sorry, but M LOOKS TOO MUCH LIKE COBOL.

I hear you :-)

The upper-case style indeed reminds me of COBOL. Note however that this is a matter of coding style and it is indeed optional. You can write M code with lower-case equally fine.

Structurally, M is closer to C and Assembly, than it could be to COBOL. Particularly when it comes to the flexibility of the language, and the ability to write complex expressions in a compact way (with the same advantages and risks that you can find when writing C).

You might also take a look at the powerful J language:

http://www.jsoftware.com/

You can download the freeware, open source interpreter and play with it.

I've played with J a few times over the years. It was mostly done by Ken Iverson, whose earlier effort was APL, and to this day I prefer APL.

APL was the first computer language I ever learned, back in the early 70s, and it just feels natural to me. J is both close enough to APL and far enough away that somehow it just doesn't feel right.

Thanks for pointing to J,
I wasn't aware of it.

It is interesting to see its database connection:
http://www.jsoftware.com/jwiki/JDB

and the iOS platform:
http://www.jsoftware.com/jwiki/Guides/iOS

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.