Exploring Basic Syntax: Variables, Functions, and Data Types

March 15, 2024

In this post, we will dive into the basic syntax of Haskell, covering variable declaration, function definition, and various data types such as integers, floats, booleans, and lists.

Variables and Declarations

In Haskell, variables are immutable, meaning once a value is assigned to a variable, it cannot be changed. To declare a variable, we use the let keyword followed by the variable name and the value it will hold. For example:

let x = 5
    y = 10

In this example, we have declared two variables x and y with the values 5 and 10, respectively.

Functions

Functions in Haskell are declared using the following syntax:

<pre><code> functionName arg1 arg2 ... argN = functionBody
</code></pre></code>

Here’s an example of a simple function that adds two numbers:

addNumbers x y = x + y

Functions can also be defined using pattern matching, guards, and recursion, which we will cover in future posts.

Data Types

Haskell has a strong type system, and it provides various data types to work with. Some of the basic data types include:

  • Integers: Whole numbers without a decimal point.
  • Floats: Numbers with a decimal point.
  • Booleans: True or False values.
  • Lists: A collection of elements of the same type.

Here are some examples of data type declarations:

let num = 5 :: Int
    piValue = 3.14 :: Float
    isTrue = True :: Bool
    myList = [1, 2, 3, 4] :: [Int]

In the above examples, we have declared variables of type Int, Float, Bool, and a list of Int.

Understanding the basic syntax of Haskell is crucial for building a strong foundation in the language. In the next post, we will explore more advanced concepts and features of Haskell.