Found 185 Articles for Haskell

Haskell Program to Check Leap Year

Akhil Sharma
Updated on 25-Apr-2023 15:34:14

308 Views

In Haskell, we can check whether a given year is a leap year is not using simple boolean expression. A leap year is a year that has an extra day (February 29th) compared to a normal year. For example, 2004 is a leap year. To determine if a year is a leap year or not, there are a few rules that must be followed. Algorithm Step 1 − The isLeapYear function is defined 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 Convert Array to Set (HashSet)

Akhil Sharma
Updated on 25-Apr-2023 15:26:36

121 Views

In Haskell, we will convert Array to Set (HashSet) by using fromList, nub and foldr functions. In the first example, we are going to use ( let set = Set.fromList arr) and in the second example, we are going to use ( let set = nub arr). And in the third example, we are going to use (let set = foldr Set.insert Set.empty arr). Algorithm Step 1 − The Data.Set module is imported to work over set. Step 2 − The program execution will be started from main function. The main() function has whole control of the ... Read More

Haskell Program to Convert File to Byte Array

Akhil Sharma
Updated on 25-Apr-2023 15:25:46

211 Views

In Haskell, we will convert File to byte array by using B.readFile function along with fromIntegral and foldl function. In the first example, we are going to use ( let byteArray = B.unpack bytes) and in the second example, we are going to use (let wordArray = map fromIntegral (B.unpack bytes) :: [Word8]). And in the third example, we are going to use (let byteArray = B.foldl' (\acc byte -> acc ++ [byte]) [] bytes). Algorithm Step 1 − The Data.ByteString modules are imported. Step 2 − The program execution will be started from main function. The main() ... Read More

Haskell Program to convert primitive types to objects

Akhil Sharma
Updated on 25-Apr-2023 15:12:55

121 Views

In Haskell, we will convert primitive types to objects by using accessor functions along with getName function, constructors and record syntax. In the first example, we are going to use (getName person = name person) and in the second example, we are going to use (getName (Person name _) = name and getAge (Person _ age) = age). And in the third example, we are going to use record syntax. Algorithm Step 1 − The ‘Person’ data type is defined with two fields I.e., Name and Age. Step 2 − The getName function is defined Step 3 − ... Read More

Haskell Program to Print Right Triangle Star Pattern

Akhil Sharma
Updated on 24-Apr-2023 11:39:31

147 Views

In haskell we can use mapM, forM as well as recursive function to create a simple right triangle star pattern. What is a Right Triangle Star Pattern? A right triangle pattern is a series of asterisks or other characters arranged in a triangular shape. In the case of a right triangle pattern, the base of the triangle is the longest side and is aligned with the horizontal axis, while the other two sides form a right angle. The number of asterisks or characters in each row of the triangle decreases as you move up the triangle, so that the top ... Read More

Haskell Program To Find The Perfect Number

Akhil Sharma
Updated on 24-Apr-2023 11:37:54

201 Views

In haskell we can use list comprehension and brute-force method to find the perfect number. What is a Perfect Number? Perfect numbers are positive integers that are equal to the sum of their proper divisors. A divisor of a positive integer n is a positive integer that divides n exactly, leaving no remainder. A proper divisor is a divisor of n that is less than n itself. For example, the proper divisors of 6 are 1, 2, and 3, and the sum of these divisors is 1 + 2 + 3 = 6. Therefore, 6 is a perfect number.. Algorithm ... Read More

Haskell Program to Check Whether a Number is Prime or Not

Akhil Sharma
Updated on 24-Apr-2023 11:37:11

335 Views

To check whether a given number is prime or not we are going to use mod function and list comprehension method in Haskell. What is a Prime Number? A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. In other words, a prime number cannot be written as the product of two smaller positive integers, except for 1 and itself. For example, the first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29. Algorithm Step 1 − The isPrime function is defined. Step 2 ... Read More

Haskell Program to Check if two strings are anagram

Akhil Sharma
Updated on 24-Apr-2023 11:36:12

250 Views

In Haskell we can check if given two strings are anagram or not using sort function and freqMap. What is Anagram? An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word "listen" is an anagram of the word "silent". Anagrams are often used in word play, puzzles, and other forms of entertainment. Algorithm Step 1 − The Data.List module is imported to use sort function. Step 2 − The isAnagram function using sort function is defined Step 3 ... Read More

Haskell Program to Check if a String is Numeric

Akhil Sharma
Updated on 14-Jul-2023 16:53:27

724 Views

In Haskell we can use read functions and Data.char library to find if a given string is numeric or not. The following example will give you a clear idea about the valid numeric values. For example, 121, 12321, and 1221 if entered as string are valid numeric values. Algorithm Step 1 − The isNumeric function using reads function is defined 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. In the main function, the string is passed to the isNumeric function. ... Read More

Haskell Program to Check Palindrome

Akhil Sharma
Updated on 24-Apr-2023 11:34:26

377 Views

This tutorial will help us in checking if any number is palindrome number or not using user-defined function and boolean functions in haskell. A palindrome number is a number that remains the same when its digits are reversed. The following example will give you a clear idea about Palindrome number. For example, 121, 12321, and 1221 are palindrome numbers, while 123, 1234, and 1212 are not. Algorithm Step 1 − The isPalindrome function using reverse function is defined as, isPalindrome str = str == reverse str. Step 2 − Program execution will be started from main function. The ... Read More

1 2 3 4 5 ... 19 Next
Advertisements