Found 185 Articles for Haskell

Haskell Program to Convert String value to byte Value

Akhil Sharma
Updated on 28-Mar-2023 15:21:13

190 Views

In Haskell, we will convert String Value to Byte value by using ord , B.pack and fromNum functions. In the first example, we are going to use (stringToBytes = map ord ) function and in the second example, we are going to call (stringToBytes = B.pack) function. And in the third example, we are going to use (byteList = map fromEnum str) function. Algorithm Step 1 − The Data.Char module is imported. Step 2 − The stringToBytes function is defined using ord function as, Step 3 − The program execution will be started from main function. The main() function ... Read More

Haskell Program to Convert Set of String to Array of String

Akhil Sharma
Updated on 28-Mar-2023 15:00:44

154 Views

In Haskell, We can use listArray and toList functions as well as list comprehension to convert set of string to array of string. In the first example, we are going to use (setToArray set = listArray (1, Set.size set) (Set.toList set)) function and in the second example, we are going to call (setToArray set = listArray (1, Set.size set) (toList set)) function directly from the main function. And in the third example, we are going to use (setToArray set = array (1, Set.size set) [(i, x) | (i, x) Array Int String setToArray set = listArray (1, Set.size set) ... Read More

Haskell Program to Convert List to Set

Akhil Sharma
Updated on 28-Mar-2023 14:59:18

350 Views

Haskell programming has internal functions like fromList, Set.fromList and foldr functions that can be used to convert list to set. In the first example, we are going to use (listToSet = Set.fromList) function and in the second example, we are going to call (set = Set.fromList xs) function directly from the main function. Whereas in third example, we are going to use (listToSet = foldr Set.insert Set.empty) function. Algorithm Step 1 − The qualified Data.Set module is imported. Step 2 − The listToSet function is defined using fromList function as, listToSet = Set.fromList. Step 3 − The program execution ... Read More

Haskell Program to Convert List to a Map

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

356 Views

In Haskell, we will convert List to a Map by using fromList and foldl functions. This can also be done using recursion and pattern matching. In the first example, we are going to use (listToMap xs = Map.fromList xs) function and in the second example, we are going to use (listToMap xs = List.foldl' (\acc (k, v) -> Map.insert k v acc) Map.empty xs) function. In the third example, we are going to use base and recursive case of the recursive function. Algorithm Step 1 − The qualified Data.Map module is imported. Step 2 − The listToMap function is ... Read More

Haskell Program to Convert String to an Array

Akhil Sharma
Updated on 28-Mar-2023 14:56:48

715 Views

In Haskell, we will convert String to an Array by using listArray and intersperse functions. In the first example, we are going to use (stringToArray s = listArray (0, length s - 1) s) function and in the second example, we are going to use (stringToArray s = s) function. In the third example, we are going to use (stringToArray s = intersperse ' ' s). Algorithm Step 1 − The stringToArray function is defined Step 2 − The program execution will be started from main function. The main() function has whole control of the program. It is written ... Read More

Haskell Program to Convert Character to String

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

962 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

341 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

221 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

116 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

82 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

Advertisements