- Published on
A Glimpse into Motoko and AgorApp embeddable IDE
- Authors
- Name
- Riccardo Biosas
- @RBiosas
Want to immediately jump start to the IDE and the coding challenge? Click here!
The Other Chapters...
- Introduction to Domain-Specific Languages
- Comparison Between Rust and Motoko
- Your First Motoko Coding Challenge
- Walkthrough of the Coding Challenge
Introduction to Domain-Specific Languages
The landscape of domain-specific languages (DSL) specifically dedicated to smart contract development is growing. The reasons why the blockchain ecosystem has fueled the research and development of so many new domain-specific languages are plentiful.
A DSL offers many attractive features and trade-offs, especially in the context of Blockchain systems - where financial assets are a first-class citizen and, subsequently, the need for security and correctness is paramount.
However, let's first define what a DSL is: a domain-specific language is a programming language that is designed to address very specific problems, platforms or domains. While languages like Javascript, Python and Golang are general-purpose and can seamlessly be used for almost anything, the design of a DSL is tightly coupled with the domain and challenges it is meant to be used for. The domain-specific design allows to achieve several benefits, such as:
- Enhanced security: by narrowing their scope, domain-specific languages can more easily integrate compile-time checks, safe type-systems and various forms of runtime protection and formal verification.
- Enhanced developer experience: similarly to the previous point, the narrow scope of DSLs allows to create abstractions and built-in that provide first-class access to the domain-specific features and functionalities.
- Enhanced performance: lastly, the highly specialized nature of DSLs allows to fine-tune the language for specific tasks in order to achieve better performance than its general-purpose counterparts.
The domain-specific language of choice in the Internet Computer ecosystem is Motoko.
Motoko is a statically typed language that compiles into WebAssembly. Its design leans towards the functional paradigm, but it equally emphasizes ease of use and smooth developer experience.
Some of its language features like pattern matching might already be familiar to those who played around with languages like Haskell, nevertheless the overall syntax is much more approachable than its functional counterparts.
The other main smart contract language in the Internet Computer landscape is Rust: unlike Motoko, Rust is obviously a general-purpose language.
Building on the Internet Computer is a decidedly different experience compared to developing on Ethereum. While the Ethereum's EVM is a sandboxed deterministic execution environment, the ICP architecture is made of canisters - computational units that execute code and store data - which can host full-fledged web-applications, make outbound HTTP requests and so on.
In the context of ICP app development, Motoko offers some notable advantages over Rust. In this article we will go through them and you will also get to practice some Motoko right away on the AgorApp embeddable IDE.
But first, an interesting tidbit for all the cyberpunk and anime enthusiasts out there: the name Motoko is likely a reference to the main character of the Ghost in the Shell series, Motoko Kusanagi - highly recommended if you haven't watched it yet!
Comparison between Rust and Motoko
The design of the Motoko programming language was tailored to easing up the asynchronous communication between different entities on the Internet Computer network. The model upon which the Internet Computer is built is that of a distributed network of memory-isolated entities that communicate asynchronously with each other. These asynchronous messages use a lingua franca called Candid.
Candid is a language-agnostic interface description language (IDL) that allows different services to interface with each other without worrying about their respective implementation details - i.e.: a frontend is written in Typescript, another service is written in Golang and an ICP application is written in Motoko.
Now, asynchronous communication in a distributed system is a notoriously daunting engineering arena. As such, the Motoko language was designed using the Actor model, which is a computational framework/paradigm that allows to easily reason about different entities in a distributed asynchronous setting.
The main actor entity on the Internet Computer is the canister. A canister is a memory-isolated entity that holds both compiled code and storage.
After being compiled into WebAssembly bytecode, Motoko programs are deployed to canisters, which then act as 'computational units' that can be invoked by other canisters or by users via the Internet Computer's IC SDK.
If you want to start dipping your toes into the Actor model and Motoko's language features, you can check out the Learn Motoko course - it is geared towards beginners, but the last lesson Introduction to Actors touches upon the very concept.
Stay tuned and subscribe to the mailing list to be the first to know when we will roll out more advanced courses!
As powerful as Rust is, it is not designed with the Actor model and the ICP architecture in mind: Motoko provides native support for Candid, a strong type system which statically ensures the absence of dynamic type errors and, lastly, more intuitive abstractions for inter-canister communication.
Enough benchmarks for now, we will revisit the topic in a future article. Let's jump right into the IDE.
Your First Motoko Coding Challenge
The challenge is nothing fancy, really: it's just a flavor of good ol' FizzBuzz.
Nevertheless, if it's your first time approaching Motoko, it's enough to quickly get the hang of the basics of its syntax: variables, loops, control flow, functions and a sprinkle of modulo arithmetic.
If Motoko captivates your interest, you got stuck during the challenge, or you just want to review Motoko syntax more in depth, check out the Learn Motoko course or come say hello to our discord.
Also, if you are a tech/web3 content creator - Udemy, Youtube, blogger- and you'd be interested in integrating our embeddable IDE in your coding tutorials, create a custom IDE instance for your audience so that they can better follow along your video courses, we'd love to hear from you.
Walkthrough of the Coding Challenge
Click on the arrow to expand the content and read the solution walkthrough.
Click to expand/collapse
Let's do a line-by-line code review provided by the IDE.
In the first line we import
import Iter "mo:base/Iter
:Iter
is a module from the Motoko base library which provides a set of useful functions for iterating over collections.func fizzBuzz(upperBound: Nat): Nat
: here we have a pretty typical function signature. The function takes aNat
as input and returns aNat
as output.Nat
stands for natural number in Motoko.var sum = 0;
: this is simply the counter that will hold the sum of all the numbers that are multiples of 3 or 5 and, at the end, will be returned as the function outputfor (i in Iter.range(1, upperBound - 1))
: theIter.range
function generates an inclusive range that goes from 1 toupperBound - 1
.if (i % 3 == 0 or i % 5 == 0)
: we use the modulo operator to check if the current number is a multiple of 3 or 5.sum += i;
: if the current number is a multiple of 3 or 5, then we add it to thesum
.sum;
: and, finally, we return thesum
as the function output.
Easy peasy, right?
Move on to the next collapsable section to see a more functionally idiomatic solution which uses recursion.
Click to expand/collapse
func fizzBuzz(upperBound: Nat): Nat {
func helper(i: Nat, sum: Nat): Nat {
if (i >= upperBound) {
return sum;
};
let newSum = if ((i % 3 == 0) or (i % 5 == 0)) {
sum + i
} else {
sum
};
helper(i + 1, newSum);
};
helper(1, 0);
}
This solution uses a recursive approach to the problem. People who solved the classic Fibonacci sequence problem will find this approach familiar.
We will review it in the next article, alongside a few more coding challenges and product updates.