Haskell Primitive Types.
Course Title: Functional Programming with Haskell: From Fundamentals to Advanced Concepts Section Title: Basic Types, Functions, and Pattern Matching Topic: Primitive types in Haskell: Int, Float, Bool, Char, String.
Overview
In this topic, we will delve into the primitive types in Haskell, which are the foundational building blocks of the language. These primitive types are the basic types that are available in Haskell, and they include integers (Int), floating-point numbers (Float), booleans (Bool), characters (Char), and strings (String). We will explore each of these types, their syntax, and provide examples to help solidify your understanding.
1. Integers (Int)
In Haskell, integers are denoted by the Int
type. They are whole numbers that can be positive, negative, or zero.
Example 1: Basic arithmetic operations with integers
-- Addition
7 + 3 -- Evaluates to 10
-- Subtraction
7 - 3 -- Evaluates to 4
-- Multiplication
7 * 3 -- Evaluates to 21
-- Division (integer division)
7 `div` 3 -- Evaluates to 2
Note that in Haskell, integer division uses the div
keyword, as shown in the example above.
2. Floating-Point Numbers (Float)
In Haskell, floating-point numbers are denoted by the Float
type. They are decimal numbers that can be positive, negative, or zero.
Example 2: Basic arithmetic operations with floating-point numbers
-- Addition
7.3 + 3.1 -- Evaluates to 10.4
-- Subtraction
7.3 - 3.1 -- Evaluates to 4.2
-- Multiplication
7.3 * 3.1 -- Evaluates to 22.63
-- Division
7.3 / 3.1 -- Evaluates to 2.35
3. Booleans (Bool)
In Haskell, booleans are denoted by the Bool
type. They are logical values that can be either True
or False
.
Example 3: Basic boolean operations
-- Logical AND
True && True -- Evaluates to True
-- Logical OR
True || False -- Evaluates to True
-- Logical NOT
not True -- Evaluates to False
4. Characters (Char)
In Haskell, characters are denoted by the Char
type. They are single characters enclosed in single quotes.
Example 4: Basic character operations
-- Defining a character
'a' -- is a single character
-- Defining a string (using square brackets)
['a', 'b', 'c'] -- is a list of characters
Note that in Haskell, characters and strings are not the same thing. A character is a single entity, while a string is a list of characters.
5. Strings (String)
In Haskell, strings are denoted by the String
type. They are lists of characters enclosed in double quotes.
Example 5: Basic string operations
-- Defining a string
"Hello World" -- is a string
-- Concatenating strings
"Hello " ++ "World" -- Evaluates to "Hello World"
Key Takeaways
- Integers are whole numbers that can be positive, negative, or zero.
- Floating-point numbers are decimal numbers that can be positive, negative, or zero.
- Booleans are logical values that can be either
True
orFalse
. - Characters are single characters enclosed in single quotes.
- Strings are lists of characters enclosed in double quotes.
Practice Exercises
- Write a Haskell function that takes an integer as input and returns its square.
- Write a Haskell function that takes two strings as input and returns their concatenation.
- Write a Haskell function that takes a boolean as input and returns its negation.
Useful Resources
Next Topic
In the next topic, we will explore working with tuples and lists in Haskell.
Leave a comment or ask for help if you have any questions or need clarification on any of the concepts covered in this topic.
Images

Comments