Found 185 Articles for Haskell

Haskell Program to Calculate the Execution Time of Methods

Akhil Sharma
Updated on 27-Mar-2023 11:42:53

461 Views

In Haskell, we can use getCurrentTime and NominalDiffTime functions to calculate the Execution Time of Methods. In the first example we are going to use (startTime

Haskell Program to calculate the power using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:42:11

405 Views

In Haskell, we can calculate the power by using recursion along with cases and also by using tail-recursion. In the first example we are going to use recursion along with base case, (power _ 0 = 1) and recursive case, (power x y = x * power x (y-1)). In the second example, we are going to use recursive cases as (power' x y | y == 0 = 1| otherwise = x * power' x (y-1)) and in third example, we are going to use tail-recursion. Algorithm Step 1 − The user defined recursive power function is defined ... Read More

Haskell Program to Reverse a Sentence Using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:39:48

204 Views

In Haskell, we can reverse a Sentence by using recursion along with concatenation and also by using list comprehension. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use concatenation as ((last sentence) : reverseSentence (init sentence)) and in third example, we are going to use list comprehension as, reverseSentence s = [s !! i | i String reverseSentence [] = [] reverseSentence sentence = (last sentence) : reverseSentence (init sentence) main :: IO () main = do let ... Read More

Haskell Program to Find G.C.D Using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:32:34

97 Views

In Haskell, we can find G.C.D by using recursion and also by using recursive case statements. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use (case (x, y) of) statement. Algorithm Step 1 − The user defined recursive gcd’ function is defined as, For example 1 − gcd' x y | y == 0 = x | otherwise = gcd' y (x `mod` y). For example 2 − ... Read More

Haskell Program to Find Factorial of a Number Using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:31:56

625 Views

In Haskell, we Find Factorial of a Number Using Recursion by using recursion along with foldl and product function. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use factorial n = foldl (*) 1 [1..n] function and third example, we are going to use factorial n = product [1..n] function. Algorithm Step 1 − The user defined recursive factorial function is defined as, For example 1 & 2 − factorial 0 = 1 factorial n = n * factorial ... Read More

Haskell Program to Find the Sum of Natural Numbers using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:30:58

157 Views

In Haskell, we Find the Sum of Natural Numbers by using recursion and tail-recursion. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use sumNat function and third example, we are going to use user-defined tail-recursive function. Algorithm Step 1 − The user defined recursive sumOfNaturalNumbers function is defined as, For example 1, 2 & 3 sumOfNaturalNumbers 0 = 0 sumOfNaturalNumbers n = n + sumOfNaturalNumbers (n - 1). For example 4 sumNat' 0 acc = acc ... Read More

Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Akhil Sharma
Updated on 27-Mar-2023 11:30:06

100 Views

In Haskell, we will Check Whether a Number can be Expressed as Sum of Two Prime Numbers by using user-defined functions. In the first example we are going to use (isPrime) function along with primeSum function and in the second and third example, we are going to use (isPrime) function along with isSumOfTwoPrimes function. In the following examples, the function isPrime checks whether a given number is prime or not. It first checks if the number is less than or equal to 1, in which case it returns False. It then checks if the number is 2, in which case ... Read More

Haskell Program to Check if An Array Contains a Given Value

Akhil Sharma
Updated on 27-Mar-2023 11:11:52

334 Views

In Haskell, we will Check if An Array Contains a Given Value by using recursion and foldr & elem functions. In the first example, we are going to use base and recursive cases and in the second example, we are going to use (containsValue x = foldr (\y acc -> acc || x == y) False) function. And in the third example, we are going to use (containsValue val arr = elem val arr) function. Algorithm Step 1 − The recursive containsValue function is defined as, For example 1 − containsValue _ [] = False containsValue ... Read More

Haskell Program to Check Whether a Character is Alphabet or Not

Akhil Sharma
Updated on 27-Mar-2023 11:03:33

528 Views

In Haskell, we will Check Whether a Character is Alphabet or Not by using relational operator comparison, if-else statements and Data. Char module. In the first example, we are going to use (isAlpha c | c >= 'a' && c = 'A' && c = 'a' && c = 'A' && c = 'a' && c = 'A' && c = 'a' && c = 'A' && c String isAlpha c | c >= 'a' && c = 'A' && c String isAlpha c = if c >= 'a' && c = 'A' && c

Haskell Program to Check Whether a Number is Positive or Negative

Akhil Sharma
Updated on 27-Mar-2023 10:59:51

756 Views

In Haskell, we can Check Whether a Number is Positive or Negative by using comparison operators and if-else statements. In the first example, we are going to use (isPositive n | n > 0 = "Positive" | n == 0 = "Zero" | otherwise = "Negative") function. And in the second example, we are going to use (isPositive n = if n > 0 then "Positive" else if n == 0 then "Zero" else "Negative") function. In the following examples, the function isPositive takes an Integer argument n, and returns a string indicating whether the number is positive, negative, or ... Read More

Previous 1 ... 4 5 6 7 8 ... 19 Next
Advertisements