How to write your first program in Mojo programming language 2024?

Mojo is a relatively new programming language that aims to combine the best features of Python and C while incorporating additional features specifically for artificial intelligence (AI) development. Here are some key points about Mojo

  1. Origins and Founding
    1. Mojo was developed by Modular Inc., a company founded by Chris Lattner, who is also the original architect of the Swift programming language.
    2. The language was first announced in May 2023 and is still in its early stages of development.
    3. Although Mojo is not yet open source, there are plans to make it open source in the future.
  2. Design Goals
    1. Mojo aims to provide a modern and concise programming language that prioritizes speed, simplicity, and maintainability.
    2. It combines the usability of Python with the performance of C.
  3. Features of Mojo
    1. Performance: Mojo is compiled to native machine code, resulting in significant performance improvements compared to interpreted Python. This makes Mojo suitable for computationally intensive tasks, especially in AI and machine learning applications.
    2. Type Safety: Mojo adopts a hybrid type approach, combining static and dynamic typing. This provides the flexibility of Python for prototyping while ensuring type safety and preventing runtime errors.
    3. Concurrency and Parallelism: Mojo supports concurrency and parallelism through features like threads and SIMD instructions, making it efficient for multi-core processors.
    4. AI-centric Design: Mojo includes features like automatic differentiation, tensor operations, model optimization, and hardware abstraction, streamlining AI model development and deployment.
    5. Portability: Mojo runs directly on target hardware platforms, eliminating the need for external libraries or frameworks.
    6. Scalability: Mojo’s architecture handles complex AI applications, supporting parallel and distributed computing.
    7. Integration with Python Ecosystem: Mojo seamlessly integrates with the Python ecosystem, allowing developers to utilize existing Python libraries and frameworks.
  4. Comparison with Python
    1. Syntax
      • Mojo: Mojo’s syntax is straightforward and concise, making writing and understanding code easy.
      • Python: Python also boasts a clean syntax, but Mojo’s simplicity is a strong point.
    2. Performance
      • Mojo: Mojo is exponentially faster than Python. It is designed for AI hardware and compiled into machine code.
      • Python: Python can be slower due to its dynamic typing and lack of low-level optimization.
    3. Ecosystem
      • Mojo: While newer, Mojo’s community is growing rapidly, and libraries/tools are becoming available.
      • Python: Python has a vast ecosystem with active development and extensive libraries.
Read more: Mojo VS Python | A comparetive analysis in 2024

Writing Your First “Hello, World!” Program in Mojo

Using the Mojo REPL (Read-Eval-Print Loop)

The Mojo REPL allows you to write and run Mojo code interactively in a command prompt. Follow these steps

  • Open your terminal.
  • Start a Mojo REPL session by typing mojo and pressing Enter.
  • Type the following expression and press Enter twice (a blank line indicates the end of an expression):
  print("Hello, world!")
  • You’ll see the output: “Hello, world!”

Example

$ mojo
Welcome to Mojo! 🔥
Expressions are delimited by a blank line. Type `:quit` to exit the REPL and `:mojo help repl` for further assistance.
1> print("Hello, world!")
2. Hello, world!

The REPL is useful for short experiments, but the code isn’t saved. For real programs, we need to write code in a .mojo source file.

Running a Mojo Source File

Now let’s create a .mojo source file and run it using the Mojo CLI

  • Create a file named hello.mojo (or any other name with the .mojo extension).
  • Add the following code to the file
fn main(): 
print("Hello, world!")
  • Save the file.
  • Return to your terminal.
  • Run the program with the following command:
 mojo hello.mojo
  • You’ll immediately see the output: “Hello, world!”

Make sure your code looks exactly like the example above, and ensure that Mojo is correctly installed.

Building an Executable Binary

For a more permanent solution, let’s build an executable binary

  • Create an executable file using the build command
mojo build hello.mojo

The resulting binary will have the same name as the .mojo file (you can change it with the -o option).

  • Run the executable
./hello

This creates a statically compiled binary containing all the necessary code and libraries. And there you have it! Your first “Hello, world!” program in Mojo. If you’re new to Mojo, I recommend exploring the language basics and experimenting with more code examples.

Leave a Comment