Skip to content

Commit

Permalink
(data) Add @Debajyati's blog to OCaml Planet (#2900)
Browse files Browse the repository at this point in the history
* add blog to planet sources

* scrape blog

* name of blog
  • Loading branch information
sabine authored Dec 29, 2024
1 parent cbcb7a7 commit 12dbfc4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data/planet-sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@
name: "Thomas Letan’s Blog"
url: https://soap.coffee/~lthms/posts/index.xml
only_ocaml: true
- id: debajyatidey
name: Debajyati's Blog
url: https://debajyatidey.hashnode.dev/rss.xml
only_ocaml: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: A Quick Introduction to the OCaml Programming Language
description: Discover a fresh approach to learn OCaml with my tutorial series. Explore
how it sets itself apart from other languages & more.
url: https://debajyatidey.hashnode.dev/a-quick-introduction-to-the-ocaml-programming-language
date: 2024-02-23T16:33:57-00:00
preview_image: https://hashnode.com/utility/r?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708705050712%2Fdc578850-7507-4a63-92d2-af3758887024.gif%3Fw%3D1200%26auto%3Dformat%2Ccompress%26gif-q%3D60%26format%3Dwebm%26fm%3Dpng
authors:
- Debajyati's OCaml Blog
source:
---

You guys who are reading this blog are maybe taught this language(OCaml) as a part of your University curriculum or just wanted to learn functional programming and started with OCaml. In either cases this blog will be beneficial.<p></p><p>This is only going to be a brief intro to OCaml, so we are not covering Data Types in this article. That will come in the next blog.</p><p>I'm trying to achieve an unconventional approach to begin the series of OCaml programming tutorials. While many tutorials typically dive into data types from the outset, I've chosen to start by discussing how OCaml is different from other languages. In this introductory phase, we'll explore fundamental aspects of the language that come into play when writing code or during compilation, such as its type inference, immutability, and expression-oriented nature.</p><p>Let's dive in!</p><h2>Let's Begin</h2><p><img src="https://media1.tenor.com/m/3AtT96QV6AUAAAAC/let-it-begin-hamster.gif" alt="Let It Begin Hamster GIF - Let It Begin Hamster Bolt GIFs" class="image--center mx-auto"></p><p>OCaml is an objective <strong>expression-oriented</strong> programming language written in C. OCaml(Objective Caml) is an open-source, general-purpose, statically typed, functional language which is actually an extension of the Caml language with support of Object oriented programming. Sounds a lot right? Don't worry if you don't understand all of terms said above. The only key-term that can be new to everyone is the term <code>expression-oriented</code>. What does it signify? Let's learn that first.</p><h2>What is an Expression-Oriented Language?</h2><p>All functional languages are expression oriented. In an expression oriented language, - nearly every code construct is an expression and thus yields a value.</p><p>In non-expression oriented languages we have two types of code constructs - statements &amp; expressions where <mark>expressions</mark> must return a value that can be used elsewhere in the code while, <mark>statements</mark> generally perform actions that do not return a value. But any statement that returns a value is already qualified to be an expression.</p><p>The best example of a non-expression oriented language is C. Examples can help understand better -</p><pre><code class="lang-c"><span class="hljs-comment">/* Statements */</span><span class="hljs-keyword">int</span> x = <span class="hljs-number">10</span>; <span class="hljs-comment">// Assignment statement</span><span class="hljs-keyword">if</span> (x &gt; <span class="hljs-number">4</span>) { <span class="hljs-comment">// Conditional Statement</span> x = <span class="hljs-number">4</span>;}<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) { <span class="hljs-comment">// Loop constructs are also statements</span> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, i);}<span class="hljs-comment">/* abs() is a standard library function in C that computes the absolute value of an integer */</span><span class="hljs-built_in">abs</span>(<span class="hljs-number">-5</span>); <span class="hljs-comment">// function calls are also statements </span><span class="hljs-comment">/* Expressions */</span><span class="hljs-keyword">int</span> sum = x + y;<span class="hljs-comment">/* Here `x+y` is an expression that evaluates to a value that is assigned to a variable `sum` */</span><span class="hljs-keyword">int</span> max = (x &gt; y) ? x : y;<span class="hljs-comment">/* Ternary operator( ?: ) is form of a conditional statement converted into an expression */</span><span class="hljs-keyword">int</span> result = <span class="hljs-built_in">abs</span>(<span class="hljs-number">-5</span>) + <span class="hljs-number">5</span>; <span class="hljs-comment">// This is an initialization expression</span></code></pre><p>In the above examples, <code>int x = 10;</code> and <code>int result = abs(-5) + 5;</code> are examples of both declaration statements &amp; initialization expression.</p><p>Also, function calls are both expressions &amp; statements because they are complete instructions that perform actions in the program but also return a value (if the return type of the function is not <code>void</code>).</p><p>So basically -</p><div class="hn-table"><table><thead><tr><td>Statement</td><td>Expression</td></tr></thead><tbody><tr><td>Statements can only be declared.</td><td>Expressions can be assigned or used as operands.</td></tr><tr><td>Statements create <a href="https://en.wikipedia.org/wiki/Side_effect_(computer_science)" target="_blank">side effects</a> to be useful.</td><td>Expressions are values or execute to values.</td></tr><tr><td>Statements are two-sided in case of execution (they may be executed or not depending on the situation).</td><td>Expressions are unique in meaning ( because they always evaluate to a value &amp; values are unique).</td></tr></tbody></table></div><p>As in an expression oriented language every statement is an expression, every piece of code returns some value.</p><h2>Strong Emphasis on Immutability</h2><p>An immutable value is a value whose state cannot be modified after it is created.</p><p>Although OCaml is not a purely functional language like HASKELL(because OCaml provides loops to use where necessary which shouldn't be in a purely functional language), OCaml strongly follows functional programming paradigm which encourages immutable data structures and values. Expressions, by their nature, are immutablethey compute values without modifying state. Mutation of states are seen as side-effects.</p><p>However it's worth noting that printing some value to the screen is also regarded as a side-effect! 😑</p><p><img src="https://media.tenor.com/y6lfLkr_aOQAAAAM/justin-timberlake-smh.gif" alt="Justin Timberlake Meme GIFs | Tenor" class="image--center mx-auto"></p><p>The good thing about immutability is it ensures data consistency by preventing unintended changes, reducing bugs caused by side effects.</p><h2>What exactly are Values in OCaml?</h2><p>Values are immutable entities representing any data in OCaml.</p><p>Unlike variables in imperative languages (like - C, Java, Python), which can change their contents over time, values in functional languages remain constant once defined.</p><pre><code class="lang-ocaml"><span class="hljs-keyword">let</span> x = <span class="hljs-number">55</span>;; <span class="hljs-comment">(* declaring x as an integer value of 55 *)</span>x = <span class="hljs-number">0</span>;; <span class="hljs-comment">(* Trying to assign 0 to x will not work. This expression will actualy check structural equality between the value of x and 0, which will evaluate to false.The `=` operator checks structural equality and `==` checks physical equality.We will cover these in upcoming articles *)</span><span class="hljs-comment">(* Then what will this expression below do? *)</span><span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;;<span class="hljs-comment">(* Trying the below code will print 10 to the screen *)</span>print_int x;;</code></pre><p>Let's try to understand what exactly happened when we redeclared x as 10. As I said before - "values are immutable". So, it isn't any mutable assignment. It seems so because of the so called '<strong>Shadow Effect</strong>'.</p><p>For now just know that every <code>let</code> binding binds a new value to a variable. Even if an old variable was declared before with the same name, the new one will shadow the former one with the same name. The old variable will still exist but remain inaccessible.</p><h2>Type Inference</h2><p>As shown in the previous examples, we declare variables in OCaml with the <code>let</code> keyword, without explicitly specifying the <strong><em>type</em></strong> of the value. But this does <strong>NOT</strong> necessarily signify that OCaml is dynamically typed.</p><p>Many people consider that OCaml is <a href="https://www.linkedin.com/advice/0/what-difference-between-strongly-weakly-typed-eqwlc#:~:text=Some%20examples%20of%20weakly%20typed,PHP,%20Perl,%20and%20C.%26text=However,%20languages%20with%20weak%20typing,handled%20dynamically%20by%20the%20interpreter." target="_blank">weakly typed</a>. Which is not true. Although implicitly typed, the compiler comes with built-in type checker that helps ensure type safety in your code; which is an essential component of OCaml's static type system. This explains that this language is a strongly typed language.</p><p><strong><mark>Type inference</mark></strong> in OCaml is a compile-time process where the type checker determines bindings' types before execution.</p><pre><code class="lang-ocaml"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Debajyati"</span>;; <span class="hljs-comment">(* The OCaml compiler will automatically inferthe type of the variable `name` as string *)</span></code></pre><p>The type checker infers types, making OCaml statically typed, though type annotations are supported.</p><pre><code class="lang-ocaml"><span class="hljs-keyword">let</span> name:<span class="hljs-built_in">string</span> = <span class="hljs-string">"Debajyati"</span>;; <span class="hljs-comment">(* Type Annotations are also supported *)</span></code></pre><h2>Recursion</h2><p>In functional programming, recursive functions are preferred over loops for most tasks. Recursive functions are more idiomatic in OCaml and can lead to more concise and readable code. Yes, it's true.</p><p>This section is for those who are already familiar with the concept of recursion. Recursion is nothing but a function calling itself from its definition. e.g. -</p><pre><code class="lang-ocaml"><span class="hljs-comment">(* This is a recursive function *)</span><span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> factorial n = <span class="hljs-keyword">if</span> n = <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> n * factorial (n-<span class="hljs-number">1</span>);;<span class="hljs-comment">(* Recursive functions are defined with the `rec` keyword after the `let` keyword. *)</span></code></pre><p>If you want to know more about functions &amp; recursions in OCaml stay tuned for my upcoming articles. : )</p><p>Recursion in OCaml promotes concise code by encapsulating repetitive patterns. It perfectly aligns with functional programming principles, holding immutable data.</p><h3>Tail Recursion</h3><p>Recursion approach can cause problems if not handled efficiently. Most programming languages implement function calls with a stack. This is called the call stack.</p><p>The size of the stack is usually limited by the operating system. Call Stack contains one element for each function call that has been started but not yet completed.</p><p>In order to perform all necessary computations without reaching a Stack overflow, - like all other functional languages, OCaml has a solution called the <strong><mark>Tail Recursion</mark></strong> or ( a more fancy term <strong><mark>Tail-Call Optimization</mark></strong>.</p><p>We will learn about this approach in detail in the upcoming blogs.</p><h2>Conclusion</h2><p>I hope this introductory article sparked some interest or added some value to your time! Let me know in the comments. We will be covering data types next and more to come in future.</p><p>Until then follow me and please share this blog with your friends. :)</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708704809275/d4557d1a-ae5d-49e2-880f-1b61bdcf7e7f.jpeg" alt="" class="image--center mx-auto"></p><p>Have a great day ahead &amp; most importantly -</p><p>Happy Coding! 🧑🏻💻 👩🏻💻</p>]]&gt;

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions data/planet/debajyatidey/data-types-in-ocaml.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions data/planet/debajyatidey/functions--solving-problems-in-ocaml.md

Large diffs are not rendered by default.

0 comments on commit 12dbfc4

Please sign in to comment.