MATL

From Esolang
Jump to navigation Jump to search

MATL /'mæt.ˌl/ is a programming language based on MATLAB/Octave and suitable for code golf.

Introduction

The MATL language is stack-oriented. Data are pushed onto and popped out of a stack. Functions may take a number of elements from the stack (usually those at the top) and push one or more outputs onto the stack. Since the functions use data that’s already present in the stack, reverse Polish (or postfix) notation is used.

To ease stack handling, values from the stack can also be copied and pasted using several clipboards. These are similar to variables in other stack-based code-golf programming languages.

The main goal in designing the language has been to keep it as close to MATLAB as possible. MATL includes functions equivalent to most commonly used MATLAB functions. It should be very easy for a MATLAB user to start programming in MATL within minutes.

Language specification and compiler

The specification document and compiler files can be found on GitHub. The compiler works in MATLAB R2015b and newer versions. Probably in older versions too, except for some specific functions. It is also compatible with Octave 4.0.0 and newer. The compiler tries to ensure consistent behaviour in both platforms.

There are also two online compilers, located at Try It Online! and MATL Online!

The language is subject to changes. This may cause old MATL code not to run in newer versions. However, old versions of the compiler and specification are kept in GitHub.

Example programs

Hello, world!

'Hello, world!'

The contents of the stack are automatically printed at the end of the program.

To run this with the compiler you need to duplicate quotation marks, as usual, and include the whole program within single quotation marks. Or type matl, then enter 'Hello, world!' on a separate line, then press enter twice.

Fibonacci sequence

Inputs a number n ≥ 2 and prints the first n Fibonacci numbers.

1ti2-:"yy+
   1        % Number literal                              
   t        % Duplicate top element                                
   i        % Input number n
   2        % Number literal
   -        % Subtract. Produces n-2
   :        % Vector of equally spaced values: [1, 2, ... , n-2]
   "        % For loop. There will be n-2 iterations
     y      %   Duplicate second-top element
     y      %   Duplicate second-top element
     +      %   Addition of top two elements
            % Implicitly end for loop
            % Implicitly display stack contents, bottom to top        

This can be run with the command matl '1ti2-:"yy+'. Or try it online!