Found 185 Articles for Haskell

Haskell Program to Check Whether a Number is Even or Odd

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

1K+ Views

In Haskell we have isEven, isOdd and mod functions that can be used for checking whether a given number is even or odd. In the first example, we are going to use (isEven 0 = True and isEven n = isOdd (n - 1)) and (isOdd 0 = False and isOdd n = isEven (n - 1)) function. And in the second example, we are going to use (isEven n = n `mod` 2 == 0) function. Method 1: Checking whether a number is even or odd using isEven and isOdd function In this method, the isEven and isOdd functions ... Read More

Haskell Program to convert the Binary number to Gray code using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:58:18

123 Views

In Haskel we can use recursion along with helper function toconvert the Binary number to Gray code. In the first example, we are going to use base case, (grayCode "" = "" and grayCode [x] = [x]) and recursive case, grayCode (x:y:xs) = x : grayCode (xs ++ [if x == y then '0' else if x == '0' then '1' else '0'])). Where as in the second example, we are going to use two helper functions along with recursion. Method 1: Converting the Binary number to Gray Code using recursion In this method, the grayCode function is defined to ... Read More

Haskell Program to convert the decimal number to binary using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:57:26

189 Views

In Haskell, we will convert the decimal number to binary by using recursion and tail-recursion. In the first example, we are going to use base case, (decToBin 0 = "0" and decToBin 1 = "1") and recursive case, (decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2)). Whereas in second example, we are going to use tail-recursive function. Algorithm Step 1 − The user defined, decToBin function is defined with base and recursive case as, Example 1 and 2 − decToBin 0 = "0" decToBin 1 = "1" decToBin n = decToBin ... Read More

Haskell Program to find the GCD of two given numbers using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:52:46

364 Views

In Haskell, we can find the the GCD of two given numbers by using recursion along with gcd function and tail-recursion. In the first and second examples, we are going to use base case, (gcd a 0 = a) and recursive case, gcd a b = gcd b (a `mod` b)) and in the third example, we are going to use tail-recursive function. In the following examples we define a gcd function that takes two Int arguments a and b. The function uses pattern matching to handle two cases − If b is 0, the function returns a ... Read More

Haskell Program to find the LCM of two given numbers using recursion

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

71 Views

In Haskell, we will find the the LCM of two given numbers by using recursion along with gcd and max function. In the first example, we are going to use (gcd) and (lcmOfTwoNumbers) function and in the second example, we are going to use (lcm) and (lcmOfTwoNumbers a b = lcm a b (max a b)) function. Algorithm Step 1 − The Prelude library is imported for hiding the gcd function. Step 2 − Define two user-defined functions - gcd and lcmOfTwoNumbers , Step 3 − Program execution will be started from main function. The main() function has whole ... Read More

Haskell Program to find the given number is PRIME or not using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:50:19

764 Views

In Haskell, we can find the given number is PRIME or not by using recursion along with helper function. In the first example, we are going to use (isPrime n | n Bool isPrime n | n Integer -> Bool isPrimeHelper n d | d > (n `div` 2) = True | n `mod` d == 0 = False | otherwise = isPrimeHelper n (d + 1) main :: IO () main = do let ... Read More

Haskell Program to find the reverse of a given number using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:48:52

221 Views

In Haskell, we can find reverse of a given number by using recursion along with helper function. In the first example, we are going to use (reverseNumHelper n rev) function and in the second example, we are going to use (reverseNum n | n < 10 = n| otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10)) function. Finding the reverse of a given number using recursion using the helper function In this method, the reverseNumHelper function takes two arguments: n and rev. n is the number to reverse and rev is ... Read More

Haskell Program to Display Prime Numbers Between Two Intervals

Akhil Sharma
Updated on 13-Mar-2023 15:27:44

167 Views

In Haskell, we can display Prime Numbers between Two Intervals using user-defined functions and list comprehension. In the first example, we are going to use (isPrime and primesInRange) user-defined functions and in the second example, we are going to use list comprehension. Algorithm Step 1 − The Data.List library is imported. Step 2 − The user-defined isPrime function is defined. Step 3 − Program execution will be started from main function. The main() function has whole control of the program. Step 4 − The variables named, “lower” and “upper” are being initialized. It will hold the range between which ... Read More

Haskell Program to Display Armstrong Number Between Two Intervals

Akhil Sharma
Updated on 13-Mar-2023 15:27:13

85 Views

In Haskell, we can useuser-defined functions and using recursion. In the first example we are going to use (user-defined, isArmstrong and armstrongInRange) function and in the second example, we are going to use recursion with base and recursive case. Algorithm Step 1 − The user-defined isArmstrong function is defined Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. Step 3 − The variables named, “lower” and “upper” are being initialized. It will hold the range between which the armstrong numbers ... Read More

Haskell Program to Display Prime Numbers Between Intervals Using Function

Akhil Sharma
Updated on 13-Mar-2023 15:26:15

108 Views

In Haskell, we can Display Prime Numbers Between Intervals using user-defined function along with filter function and recursion. In the first example we are going to use user-defined, (isPrime) function with (primeInRange a b = filter isPrime [a..b] ) function and in the second example, we are going to use recursion with base and recursive case. Algorithm Step 1 − The Data.List library is imported. Step 2 − The user-defined isPrime function is defined as, Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as ... Read More

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