Found 185 Articles for Haskell

Haskell Program to Print Downward Triangle Star Pattern

Akhil Sharma
Updated on 28-Mar-2023 12:45:23

96 Views

In Haskell, to print the downward traingle star pattern we will be using mapM_, replicate and unlines functions. In the first example, we are going to use ( mapM_ putStrLn $ map (concat . flip replicate "*") rows) function defined under main function and in the second example, we are going to use user-defined, (printDownwardTriangle n = mapM_ putStrLn [replicate i '*' | i

Haskell Program to Print Left Triangle Star Pattern

Akhil Sharma
Updated on 28-Mar-2023 12:43:39

191 Views

In Haskell, to print the Left Triangle Star Pattern we will be using mapM_ function, replicate function and recursion. In the first example, we are going to use ( mapM_ putStrLn [replicate i '*' | i > putStrLn (replicate n '*') else return ()) function. Method 1: Printing Left Triangle Star Pattern using mapM_ function under main function In this method, we first define the number of rows of the triangle as n (in this case, n = 5). We then use a list comprehension to generate a list of strings, where each string is a row of the ... Read More

Haskell Program to Check Whether an Alphabet is Vowel or Consonant

Akhil Sharma
Updated on 28-Mar-2023 12:40:40

240 Views

We can use elem function in Haskell to check whether the given alphabet is vowel or consonant. In the first example, we are going to use (isVowel c = c `elem` "aeiouAEIOU") function. And in other examples, we’re going to use elem function along with certain if-else statements, nested conditions and guards. Algorithm Step 1 − The data type ‘Person’ is defined with two fields name and an age. Step 2 − The isVowel function is defined using elem function as Step 3 − The program execution will be started from main function. The main() function has whole control ... Read More

Haskell Program to Call One Constructor from Another

Akhil Sharma
Updated on 28-Mar-2023 12:26:32

145 Views

In Haskell, we will call one constructor from another by using user-defined function. In the first example, we are going to use (newPerson name = Person name 10) constructor and in the second example, we are going to use (newPerson n a = Person { name = n, age = a }) constructor. In the third example, we are going to use (clonePerson p = p { name = name p ++ " clone" }) constructor and in the fourth example, we are going to use (clonePerson p = p { name = name p ++ " clone" }) ... Read More

Haskell Program to get total Bits Required for the Given Number using Library Function

Akhil Sharma
Updated on 28-Mar-2023 12:22:59

43 Views

Haskell has internal function like finiteBitSize, ceiling, logBase, length and showIntAtBase that can be used to get total bits required from the given number. In the first example, we are going to use (bits = finiteBitSize (fromIntegral x :: Int)) function and in the second example, we are going to use (bitsRequired n = ceiling (logBase 2 (fromIntegral (abs n) + 1))) function. In the third example, we are going to use (bitsRequired n = length $ showIntAtBase 2 intToDigit (abs n) "") function. Algorithm Step 1 − Import the internal libraries Step 2 − The bitsRequired function ... Read More

Haskell Program to Calculate the Logarithm Gamma of the Given Number

Akhil Sharma
Updated on 28-Mar-2023 12:20:36

79 Views

In Haskell, we will calculate the logarithm gamma of the given number by using Stirling’s approximation and Lanczos appromixation formula. In the first example, we are going to use Stirling’s approximation with (s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)) function and in the second example, we are going to use Lanczos approximation formula along with (lanczos = log $ ... Read More

Haskell Program to Round a Number to n Decimal Places

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

622 Views

In Haskell, we can use round, printf and truncate functions to round a number to n decimal places. In the first example, we are going to use (roundTo n x = (fromInteger $ round $ x * (10^n)) / (10.0^^n)) function and in the second example, we are going to use (roundTo n x = read $ printf ("%." ++ show n ++ "f") x) function.In the third example, we are going to use (roundTo n x = fromIntegral (truncate $ x * 10^n) / 10^n). Algorithm Step 1 − The roundTo function is defined using round function Step ... Read More

Haskell Program to Find Sum of N Numbers Using Recursion

Akhil Sharma
Updated on 27-Mar-2023 11:44:50

459 Views

In Haskell, we can find Sum of N Numbers by using recursion, tail-recursion and fold-recursion. In the first example we are going to use base case, (sum_n [] = 0) and recursive case, (sum_n (x:xs) = x + sum_n xs)) and in second example, we are going to use, tail-recursion. And in the third example, we are going to use (sumOfN''' xs = foldr (+) 0 xs) function. Algorithm Step 1 − The recursive sum_n function is defined as, For example 1 − sum_n [] = 0 sum_n (x:xs) = x + sum_n xs. ... Read More

Haskell Program to Find the Product of Two Numbers Using Recursion

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

271 Views

In Haskell, we can find the Product of Two Numbers by using recursion along with recursive repeated addition. In the first example we are going to use (product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1)) function. And in the second example, we are going to use recursive repeated addition. Algorithm Step 1 − The recursive product’ function is defined as, For example 1 and 2 − product' x y | y == 0 = 0 ... Read More

Haskell Program to Find Sum of Digits of a Number using Recursion

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

338 Views

In Haskell, we can find Sum of Digits of a Number by using recursion along with mod, div and other helper functions. getCurrentTime and NominalDiffTime function. In the first example we are going to use (sumOfDigits n | n < 10 = n | otherwise = (n `mod` 10) + sumOfDigits (n `div` 10)) function. And in the second example, we are going to use helper function. Algorithm Step 1 − The recursive sumOfDigits function is defined as, For example 1 − sumOfDigits n | n < 10 = n ... Read More

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