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

  1. Primitive Data Types:

    • Int
    • Integer
    • Float
    • Double
    • Char
    • Bool
  2. Type Declarations:

    • How to declare variables with specific types.
    • Type inference in Haskell.
  3. 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.
-- Example of Int
x :: Int
x = 42

-- Example of Integer
y :: Integer
y = 12345678901234567890

Float and Double

  • Float: A single-precision floating-point number.
  • Double: A double-precision floating-point number, which provides more precision than Float.
-- Example of Float
a :: Float
a = 3.14

-- Example of Double
b :: Double
b = 3.141592653589793

Char

  • Char: Represents a single Unicode character.
-- Example of Char
c :: Char
c = 'A'

Bool

  • Bool: Represents a boolean value, which can be either True or False.
-- Example of Bool
d :: Bool
d = True

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 an Int to a more general numeric type.
  • round, ceiling, floor: Convert floating-point numbers to integers.
  • show: Converts a value to a String.
  • read: Converts a String 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.

addThreeNumbers :: Int -> Int -> Int -> Int
addThreeNumbers a b c = a + b + c

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.

convertAndAdd :: Int -> Int -> Float
convertAndAdd x y = fromIntegral x + fromIntegral y

Exercise 3: String Manipulation

Write a function greet that takes a String representing a name and returns a greeting message.

greet :: String -> String
greet name = "Hello, " ++ name ++ "!"

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.

© Copyright 2024. All rights reserved