Found 26504 Articles for Server Side Programming

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

203 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

965 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

1K+ 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

2K+ 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

Haskell Program to Check Whether a Number is Even or Odd

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

3K+ 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

221 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

350 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

778 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

178 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

2K+ 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

Advertisements