# Join the M revolution – M and R programming languages Image by :
Opensource.com

## Subscribe now

Get the highlights in your inbox every week.

“'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?