Found 185 Articles for Haskell

Haskell Program to get the Division and Remainder using library function

Akhil Sharma
Updated on 13-Mar-2023 14:56:52

813 Views

In Haskell, we can use divMod and quotRem functions to get the division and remainder of a given number. In the first example, we are going to use (divMod x y) function and in the second example, we are going to use (quotRem 20 7) function. Algorithm Step 1 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. Step 2 − The variables named, “x” and “y” are being initialized. The interal function will divide them by taking them as argument. Step ... Read More

Haskell Program to get the real part from a Complex number

Akhil Sharma
Updated on 13-Mar-2023 14:55:40

71 Views

In Haskell, we can use realPart function and pattern matching to get the real part from a complex number. In the first example we are going to use (realPart c) function and in the second example, we are going to use patter matching as (x:+_) = c. Algorithm Step 1 − The Data.Complex module is imported to work over the complex numbers. 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 variable named, “c” is being ... Read More

Haskell Program to check a given number is finite or not

Akhil Sharma
Updated on 13-Mar-2023 14:54:26

95 Views

In Haskell, we can use isIEEE, isInfinite and isNaN functions to check whether a given number is finite or not. In the first example we are going to use (isIEEE n) function with if-else statement and in the second example, we are going to use (isInfinite n) function. And in third example, we are going to use (isNaN) function along with (isInfinite n) function. Algorithm Step 1 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. Step 2 − The variable named, ... Read More

Haskell Program to get the numerator from a rational number

Akhil Sharma
Updated on 13-Mar-2023 14:53:00

91 Views

In Haskell, we can use numerator, div, quot and gcd functions to find the numerator from a given rational number. In the first example we are going to use numerator (r) function and in the second example, we are going to use (n `div` gcd n d) function. And in third example, we are going to use (numerator r `quot` gcd (numerator r) (denominator r)) function. Algorithm Step 1 − The Data.Ratio module is imported to use numerator function. Step 2 − Program execution will be started from main function. The main() function has whole control of the program. ... Read More

Haskell Program to find the LCM using library function

Akhil Sharma
Updated on 13-Mar-2023 14:51:59

71 Views

In Haskell, we can use GCD function to find the LCM of the given number. In the first example we are going to use (a * b) `div` (gcd a b) function and in the second example, we are going to use foldl' (\x y -> (x*y) `div` (gcd x y)) function. In third example, we are going to use gcd' b (a `mod` b) function. Algorithm Step 1 − The lcm function is defined using gcd function as, lcm a b = (a * b) `div` (gcd a b). Step 2 − Program execution will be started ... Read More

Haskell Program to find the GCD using library function

Akhil Sharma
Updated on 13-Mar-2023 14:50:14

136 Views

In Haskell, we will find the GCD using library function like gcd, div function and recursion. In the first example, we are going to use gcd (a b) function and in the second example, we are going to use (a `div` b) function. In third example, we are going to use recursion. Method 1: Finding the GCD using gcd function In this method, the gcd function takes two integers as input and returns the greatest common divisor of these two numbers. This function is defined in the Prelude library. Algorithm Step 1 − Program execution will be started from ... Read More

Haskell Program to check the given number is an EVEN number using library function

Akhil Sharma
Updated on 13-Mar-2023 14:48:37

535 Views

In Haskell, we can use library function like mod, even and quot to check whether, the given number is an EVEN number or not. In this article we are going to have examples that are using n `mod` 2 == 0 even function as well as n `quot` 2 == 0 function. Algorithm Step 1 − The isEven function is defined using mod function as, isEven n = n `mod` 2 == 0. Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main ... Read More

Haskell Program to create a simple recursive function

Akhil Sharma
Updated on 01-Mar-2023 11:06:59

639 Views

In this article, we are going to understand how to create a simple function in Haskell using user-defined function. Here, the user-defined recursive functions are defined that will contain base case and recursive case. Then, these functions are being called recursively by passing the argument to it. In all the examples, we are going to define certain functions to perform certain tasks. All these functions include, base and recursive cases. The function defined includes factorial, fibonacci, sumList, etc. Algorithm Step 1 − The user defined recursive function is defined with base and recursive case. Step 2 − ... Read More

Haskell Program to return multiple values from the function

Akhil Sharma
Updated on 01-Mar-2023 11:06:28

838 Views

In Haskell, we can return multiple values from the function by using user-defined function along with tuple, custom data type and Maybe data type. In the first example, we are going to use (myFunction = ("Hello, World!", 42)) and in the second example, we are going to use (data MyData = MyData { stringValue :: String, intValue :: Int }). And in third example, we are going to use, (myFunction :: Maybe (String, Int)). Method 1: Using Tuple In this method, a function can return multiple values by using a tuple. A tuple is a data structure that holds multiple ... Read More

Haskell Program to return a string from the function

Akhil Sharma
Updated on 01-Mar-2023 11:05:22

683 Views

In this article, we are going to learn how to return a string from a function using user-defined function along with record syntax and let binding. In the first example, we are going to use (myFunction = "Hello, World!") function and in the second example, we are going to use (myFunction = stringValue myData). And in third example, we are going to use let binding, (myFunction = let str = "Using let binding!" in str). Method 1: Returning a string from the user-defined function In this method, the user-defined functions are defined that will contain the function definition and return ... Read More

Advertisements