Programming Articles - Page 497 of 3363

Haskell Program to calculate the sum of all even numbers

Akhil Sharma
Updated on 19-Jan-2023 14:36:52

673 Views

This tutorial will help us in calculating the sum of all even numbers. Haskell uses a functional programming paradigm, which means that it uses functions to transform data, rather than using loops and variables to keep track of state changes. There are different ways to calculate the sum of all even numbers between 1 and 100 in Haskell. Algorithm Step 1 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. Step 2 − The list comprehension [x | x

Haskell Program to check whether a variable is defined or not

Akhil Sharma
Updated on 19-Jan-2023 14:33:38

693 Views

This tutorial will help us in checking whether a variable is defined or not. In Haskell, a variable is considered defined if it has a value assigned to it. The value can be of any type, including a special type called Maybe that is used to represent values that may or may not be present. Algorithm Step 1 − The Data.Maybe module is imported. Step 2 − The checkDefined function is defined as, checkDefined x = isJust x. It takes a Maybe value and returns a Bool indicating whether the value is Just (i.e. defined) or Nothing (i.e. not ... Read More

Haskell Program to Check the birthday and print Happy Birthday message

Akhil Sharma
Updated on 19-Jan-2023 14:30:26

410 Views

In Haskell, this program will help us in checking the current date against a specified birthday and prints a "Happy Birthday" message if it is a match otherwise, it will print “Not your birthday yet”. Haskell provides function to fetch the current date and also to specify a date. This program can be implemented by using various approaches including the use of toGregorian function, fromGregorian function or by using utctDay getCurrentTime. This program will only check the birthday once, when the program runs. If we want the program to check the birthday periodically or at a specific time, we ... Read More

Haskell Program to Iterate over enum

Akhil Sharma
Updated on 19-Jan-2023 14:26:22

2K+ Views

In Haskell, an enumeration (or "enum") is a type that has a finite set of values. The values are called constructors, and are usually defined using the data keyword. Here's an example of an enumeration type that represents the four seasons − data Season = Spring | Summer | Fall | Winter Enumerations are used to represent things like states, modes, or options that have a fixed set of possible values. Method 1: Iterate over enumeration type This method is used to directly iterate over the enumeration type in Haskell. Here, we define the list of all the enumeration ... Read More

Haskell Program to Implement multiple inheritance

Akhil Sharma
Updated on 19-Jan-2023 14:01:11

599 Views

Haskell is a functional programming language and does not have a concept of inheritance. Instead, it uses type classes to achieve similar functionality. This tutorial will help us in implementing the same in Haskell. Method 1: Implement a type class that simulates multiple inheritances. This method uses type class to simulate the multiple inheritance. Here, we define two parent class and one child class. And the function of child class is being called. Once the function is being called it will take instance from both the parent class and the final output is displayed. Algorithm Step 1 − The ... Read More

Haskell Program to initialize and print a complex number

Akhil Sharma
Updated on 19-Jan-2023 13:37:01

284 Views

This tutorial will help us in initializing and printing a complex number. In Haskell, the Data.Complex library provides a Complex type to represent complex numbers. Method 1: Using Complex data type This method defines a Complex data type that holds the real and imaginary parts of a complex number, and an instance of the Show type class for Complex, which allows it to be printed using the putStrLn function. In the main function, it creates a complex number object with real part and imaginary part. Then it prints the complex number using the putStrLn function and the show function. Algorithm ... Read More

Swift Program to remove all 'nil' elements from the array

Ankita Saini
Updated on 17-Jan-2023 17:36:39

2K+ Views

In this article, we will learn how to write a swift program to remove all ‘nil’ elements from the array. Till now we all know that an array can store elements of a single data type. But Swift array can also hold elements of multiple data types. To store multiple data types in the array we use [Any], where [Any] specifies that the address can hold elements of any data types. Similarly an array also contain nil value. Here we use the following methods to remove nil elements from the array. Using compactMap() function Using filter() function ... Read More

Swift Program to find the prime numbers from the array

Ankita Saini
Updated on 17-Jan-2023 17:40:27

2K+ Views

In this article, we will learn how to write a swift program to find the prime numbers from the array. Prime numbers are those numbers that are only divisible by 1 and itself. Or we can say prime number have only two factors that are 1 and the number itself. For example, 1, 2, 7, 3, 11, 31, etc. So to find the prime numbers from the given array, we individually check each element of the given array is prime or not. Algorithm Step 1 − Create a function. Step 2 − Check if the number is greater than ... Read More

Swift Program to find the EVEN numbers from the array

Ankita Saini
Updated on 17-Jan-2023 17:20:51

2K+ Views

In this article, we will learn how to write a swift program to find even numbers from the array. Even number are those number which are completely divisible by 2. For example, 2, 6, 50, 20, etc. Here we use the following methods to find the even numbers from the array. Using for-in loop Using filter() function Method 1: Using for-in loop To find the even numbers from the given array we use for-in loop. Using for-in loop we iterate through each element of the given array and check if the element is even number or not. ... Read More

Swift Program to fetch elements from an array based on an index

Ankita Saini
Updated on 17-Jan-2023 17:15:02

485 Views

In this article, we will learn how to write a swift program to fetch elements from an array based on an index. Using array syntax An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. In an array, every element has an index. The array index is start from 0 and goes up to N-1. Here N represent the total number of array elements. To retrieve elements from the array according to their index we can use subscript syntax. ... Read More

Advertisements