How to Perform Basic Arithmetic Operations In Haskell?

7 minutes read

In Haskell, you can perform basic arithmetic operations like addition, subtraction, multiplication, and division using simple functions. Here's how you can do it:

  1. Addition (+): To add two numbers, you simply use the "+" operator between them. For example, to add 2 and 3, you would write: 2 + 3.
  2. Subtraction (-): To subtract one number from another, you use the "-" operator between them. For example, to subtract 3 from 7, you would write: 7 - 3.
  3. Multiplication (): To multiply two numbers, you use the "" operator between them. For example, to multiply 4 and 5, you would write: 4 * 5.
  4. Division (/): To divide one number by another, you use the "/" operator between them. For example, to divide 10 by 2, you would write: 10 / 2.


It's important to note that these operations work with numbers of the same type. For instance, you cannot add an integer and a floating-point number directly.

Best Haskell Books to Read in 2024

1
Programming in Haskell

Rating is 5 out of 5

Programming in Haskell

2
Get Programming with Haskell

Rating is 4.9 out of 5

Get Programming with Haskell

3
Real World Haskell

Rating is 4.8 out of 5

Real World Haskell

4
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

Rating is 4.7 out of 5

Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

5
Haskell from the Very Beginning

Rating is 4.6 out of 5

Haskell from the Very Beginning

6
Programming in Haskell

Rating is 4.5 out of 5

Programming in Haskell


What is exponentiation in Haskell?

Exponentiation in Haskell refers to raising a number to a power. Haskell provides the ^ and ** operators for exponentiation.


The ^ operator is used for integer exponentiation and has the following syntax:

1
(^) :: (Num a, Integral b) => a -> b -> a


For example, 2 ^ 3 evaluates to 8 because 2 raised to the power of 3 is 8.


The ** operator is used for fractional exponentiation and has the following syntax:

1
(**) :: Floating a => a -> a -> a


For example, 2.0 ** 0.5 evaluates to 1.41421356 because 2.0 raised to the power of 0.5 (square root) is approximately 1.41421356.


How to generate a random number in Haskell?

In Haskell, you can generate random numbers using the random and randomR functions from the System.Random module. Here's how you can do it:

  1. Import the required module by adding import System.Random at the top of your file.
  2. Create a random number generator using the mkStdGen function. This function takes an integer seed as an argument. You can use any value as the seed, such as the current time, to generate different random numbers each time your program runs.
1
2
gen :: StdGen
gen = mkStdGen seed


  1. Use the random function to generate a random number of any type. This function takes a generator as an argument and returns a tuple containing a random value and a new generator. You can use pattern matching to extract the random value and the new generator from the tuple.
1
(randomValue, newGen) = random gen :: (Int, StdGen)


Note: The type signature (Int, StdGen) in the above line indicates that we want to generate a random Int value. You can replace it with any other type, such as Double, Bool, or a custom data type.

  1. If you want to generate a random number within a specific range, you can use the randomR function instead. This function takes a range (bounded by a minimum and maximum value) and a generator as arguments, and returns a tuple containing a random value within the range and a new generator.
1
(randomValueInRange, newGenInRange) = randomR (minValue, maxValue) gen :: (Int, StdGen)


Here's a complete example that generates a random number between 1 and 100:

1
2
3
4
5
6
7
import System.Random

main :: IO ()
main = do
  let gen = mkStdGen seed
      (randomValue, _) = randomR (1, 100) gen :: (Int, StdGen)
  putStrLn $ "Random number: " ++ show randomValue


Remember to replace seed with any integer value you like, or use a different approach to generate the seed.


What is the function for calculating the greatest common divisor in Haskell?

The function for calculating the greatest common divisor in Haskell is gcd. Here is its signature and an example usage:

1
gcd :: Integral a => a -> a -> a


Example:

1
2
3
4
gcdResult :: Int
gcdResult = gcd 36 48

-- Output: 12


In the example above, the gcd function is used to calculate the greatest common divisor of 36 and 48, which is 12.

Facebook Twitter LinkedIn Telegram

Related Posts:

Working with Input/Output (IO) in Haskell involves performing operations that interact with the outside world, such as reading and writing to files, reading user input from the console, or making network requests. Haskell treats IO as a separate monadic system...
To use libraries and packages in Haskell, you need to follow these steps:Install the Haskell build tool, Cabal, on your system. Cabal allows you to easily manage dependencies and build Haskell projects. Identify the library or package you want to use. You can ...
Concurrent programming in Haskell involves writing code that can execute multiple computations simultaneously. Haskell provides several abstractions and features to facilitate concurrent programming. Here are some key concepts and techniques used in concurrent...