Found 26504 Articles for Server Side Programming

Haskell Program to Convert Character to String

Akhil Sharma
Updated on 28-Mar-2023 14:55:14

2K+ Views

In Haskell, we can convert Character to String by using user-defined function, show function, list comprehension and (:[]) notation. In the first example, we are going to use (charToString c = [c]) function and in the second example, we are going to use (charToString c = show c) function. Where as in third, we are going to use (charToString c = [x | x String charToString c = [c] main :: IO () main = do let myChar = 'a' let myString = charToString myChar putStrLn myString Output [1 of 1] ... Read More

Haskell Program to Convert Data to Hexadecimal

Akhil Sharma
Updated on 28-Mar-2023 14:51:47

723 Views

In Haskell, we will convert Data to Hexadecimal by using intToDigit, toHex and showHex functions. In the first example, we are going to use toHex function and (stringToHex = concatMap (\c -> toHex (fromEnum c))) function and in the second example, we are going to use convertToHex x = showHex x ""). And in the third example, we are going to use (toHex = concatMap toHexChar). Method 1: Converting Data to Hexadecimal using intToDigit and toHex function In this method, the toHex function converts an integer to its hexadecimal representation by repeatedly dividing by 16 and converting each remainder ... Read More

Haskell Program to Print Inverted Star Pattern

Akhil Sharma
Updated on 28-Mar-2023 14:36:40

329 Views

In Haskell, to print Inverted star pattern we will be using mapM_, reverse, unlines and replicate functions. In the first example, we are going to use ( invertedStarPattern n = mapM_ putStrLn (reverse [replicate i '*' | i

Haskell Program to Print Diamond Star Pattern

Akhil Sharma
Updated on 28-Mar-2023 14:34:57

232 Views

In Haskell, to print Diamond star pattern we will be using mapM_, reverse, unlines and replicate functions. In the first example, we are going to use ( diamondStarPattern n = unlines $ topHalf ++ bottomHalf where topHalf = [replicate (n - i) ' ' ++ replicate ((2 * i) - 1) '*' | i

Haskell Program to Print Mirror Lower Star Triangle Pattern

Akhil Sharma
Updated on 28-Mar-2023 12:48:38

158 Views

In haskell, we can use unlines and replicate functions to print mirror lower star in triangle pattern. In the first example, we are going to use ( mirrorLowerTriangle n = unlines [replicate i '*' | i

Haskell Program to Print Downward Triangle Star Pattern

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

194 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

317 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

582 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

250 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

120 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

Advertisements