In this section, we will explore the fundamental data types in Haskell. Understanding these basic data types is crucial as they form the building blocks for more complex data structures and functions.
Key Concepts
-
Primitive Data Types:
Int
Integer
Float
Double
Char
Bool
-
Type Declarations:
- How to declare variables with specific types.
- Type inference in Haskell.
-
Type Conversion:
- Converting between different data types.
Primitive Data Types
Int and Integer
- Int: A fixed-precision integer. It has a limited range, which depends on the system architecture (usually 32 or 64 bits).
- Integer: An arbitrary-precision integer. It can represent very large numbers but is less efficient than
Int
.
Float and Double
- Float: A single-precision floating-point number.
- Double: A double-precision floating-point number, which provides more precision than
Float
.
Char
- Char: Represents a single Unicode character.
Bool
- Bool: Represents a boolean value, which can be either
True
orFalse
.
Type Declarations
In Haskell, you can explicitly declare the type of a variable using the ::
operator. However, Haskell's type inference system often allows you to omit these declarations.
-- Explicit type declaration name :: String name = "Haskell" -- Type inference age = 30 -- Haskell infers that age is of type Int
Type Conversion
Sometimes, you need to convert between different data types. Haskell provides several functions for type conversion.
fromIntegral
: Converts anInt
to a more general numeric type.round
,ceiling
,floor
: Convert floating-point numbers to integers.show
: Converts a value to aString
.read
: Converts aString
to a value of a specified type.
-- Converting Int to Float intToFloat :: Int -> Float intToFloat x = fromIntegral x -- Converting Float to Int floatToInt :: Float -> Int floatToInt x = round x -- Converting Int to String intToString :: Int -> String intToString x = show x -- Converting String to Int stringToInt :: String -> Int stringToInt x = read x
Practical Examples
Example 1: Basic Arithmetic Operations
-- Adding two Int values sumInts :: Int -> Int -> Int sumInts a b = a + b -- Multiplying two Float values multiplyFloats :: Float -> Float -> Float multiplyFloats x y = x * y
Example 2: Working with Characters and Strings
-- Concatenating two Strings concatStrings :: String -> String -> String concatStrings str1 str2 = str1 ++ str2 -- Checking if a Char is a digit isDigit :: Char -> Bool isDigit ch = ch >= '0' && ch <= '9'
Exercises
Exercise 1: Basic Arithmetic
Write a function addThreeNumbers
that takes three Int
values and returns their sum.
Exercise 2: Type Conversion
Write a function convertAndAdd
that takes two Int
values, converts them to Float
, adds them, and returns the result as a Float
.
Exercise 3: String Manipulation
Write a function greet
that takes a String
representing a name and returns a greeting message.
Summary
In this section, we covered the basic data types in Haskell, including Int
, Integer
, Float
, Double
, Char
, and Bool
. We also discussed type declarations, type inference, and type conversion. Understanding these fundamental concepts is essential for working with more complex data structures and functions in Haskell.
Haskell Programming Course
Module 1: Introduction to Haskell
- What is Haskell?
- Setting Up the Haskell Environment
- Basic Syntax and Hello World
- Haskell REPL (GHCi)