In Haskell, you can perform basic arithmetic operations like addition, subtraction, multiplication, and division using simple functions. Here's how you can do it:
- Addition (+): To add two numbers, you simply use the "+" operator between them. For example, to add 2 and 3, you would write: 2 + 3.
- 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.
- Multiplication (): To multiply two numbers, you use the "" operator between them. For example, to multiply 4 and 5, you would write: 4 * 5.
- 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.
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:
- Import the required module by adding import System.Random at the top of your file.
- 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 |
- 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.
- 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.