Found 185 Articles for Haskell

Haskell Program to Make a Simple Calculator Using switch...case

Akhil Sharma
Updated on 13-Mar-2023 15:25:32

542 Views

In this article we are going to learn how to make a simple calculator in Haskell using switch…case. In the first example, we are going to use case statements with different operators and in the second example, we are going to use map of functions as (fromList [('+', (+)), ('-', (-)), ('*', (*)), ('/', (/))]). Method 1: Program to make a simple calculator using case statement In this method, a simple calculator program in Haskell is formed which takes three arguments, x, op and y. The x and y are of type Double and op is of type Char. The ... Read More

Haskell Program to Display Factors of a Number

Akhil Sharma
Updated on 13-Mar-2023 15:23:45

350 Views

In Haskell, we can list comprehension, filter function and recursion to display factors of a number. In the first example we are going to use (factors n = [x | x n `mod` x == 0) [1..n]) function. And in third example, we are going to use recursion with base and recursive case. Algorithm Step 1 − The user-defined factors function is defined using internal functions. 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 ... Read More

Haskell Program to Display Armstrong Numbers Between Intervals Using Function

Akhil Sharma
Updated on 13-Mar-2023 15:22:33

85 Views

Haskell has functions like higher order and filter, that can be used for getting the Armstrong number between two given internvals. In the first example we are going to use (isArmstrong and armstrongInRange function with higher order) and in the second example, we are going to use (filter isArmstrong [a..b]) function. Algorithm Step 1 − The user-defined isArmstrong 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. Step 3 − The variables named, “lower” and “upper” are ... Read More

Haskell Program to compare numbers and strings using library function

Akhil Sharma
Updated on 13-Mar-2023 15:41:02

703 Views

In Haskell, we will compare numbers and strings using library function using compare, min and max functions. Also, by using Eq and Ord typeclasses. In the first example we are going to use (compare num1 num2 and compare str1 str2) function and in the second example, we are going to use (max num1 num2 and min str1 str2) function. And in third example, we are going to use (num1 == num2) function along with (num1 /= num2) typeclasses. Algorithm Step 1 − The Data.Ord library is imported, which contains the compare function. Step 2 − Program execution ... Read More

Haskell Program to get the remainder of float numbers using library function

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

199 Views

Haskell has internal functions like divMod, mod and properFraction to get the remainder of the float number. In the first example we are going to use (divMod' (x) (y)) function and in the second example, we are going to use (mod' x y) function. And in third example, we are going to use (properFraction $ x / y) function. Algorithm Step 1 − The Data.Fixed module is imported to use divMod function. Step 2 − The variables named, “x” and “y” are being initialized. It will hold the floating point numbers whose remainder value is to be found ... Read More

Haskell Program to get the Magnitude of the given number

Akhil Sharma
Updated on 13-Mar-2023 15:20:47

142 Views

Haskell has internal functions like negate, abs and signum functions that can be used to get magnitude of the given number. In the first example we are going to use (negate n) function and in the second example, we are going to use (abs) function. In third example, we are going to use (signum x) function. Algorithm Step 1 − Define the magnitude function Step 2 − Program execution will be started from main function. The main() function has whole control of the program. Step 3 − The variable named, “num” is being initialized. It will hold the number ... Read More

Haskell Program to get the denominator from a rational number

Akhil Sharma
Updated on 13-Mar-2023 15:18:50

162 Views

In Haskell, we can use denominator, div, quot and gcd functions to obtain the denominator from a rational number. In the first example we are going to use (denominator r) function and in the second example, we are going to use (d `div` gcd n d) function. Algorithm Step 1 − The Data.Ratio module is imported to use denominator function. 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. It calls the denominator function with the rational number and prints ... Read More

Haskell Program to convert boolean variables into string

Akhil Sharma
Updated on 13-Mar-2023 15:17:55

173 Views

In Haskell, we will convert boolean variables into string by using user-defined function, boolToString along with if-else statement and pattern matching. In the first example, we are going to use (boolToString b = show b) function and in the second example, we are going to use (boolToString b = if b then "yes" else "no"). And in third example, we are going to use pattern matching. Algorithm Step 1 − Define the boolToString function Step 2 − The program execution will be started from main function. The main() function has whole control of the program. It is written as ... Read More

Haskell Program to convert int type variables to long

Akhil Sharma
Updated on 13-Mar-2023 15:16:11

182 Views

In Haskell, we can use internal functions like fromIntegral intToLong and toInteger function and toEnum to convert int type variable to long. In the first example, we are going to use (let longVar = fromIntegral intVar :: Int64) and in the second example, we are going to use (intToLong = toEnum) function. Algorithm Step 1 − The Data.Int module is imported. Step 2 − The program execution will be started from main function. The main() function has whole control of the program. Step 3 − The variable named, “intVar” is being initialized. It will hold the Int type variable ... Read More

Haskell Program to convert long type variables into int

Akhil Sharma
Updated on 13-Mar-2023 15:13:57

146 Views

In Haskell, we can use fromIntegral function along with toInteger and truncate function, div and mod functions to convert long type variable into int. In the first example, we are going to use ( let intVar = fromIntegral longVar :: Int) and in the second example, we are going to use (let highPart = fromIntegral (longVar `div` 2^32) :: Int and let lowPart = fromIntegral (longVar `mod` 2^32) :: Int). Algorithm Step 1 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main ... Read More

Previous 1 ... 6 7 8 9 10 ... 19 Next
Advertisements