Programming Articles - Page 433 of 3363

Haskell Program to Call One Constructor from Another

Akhil Sharma
Updated on 28-Mar-2023 12:26:32

273 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

148 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

Haskell Program to Calculate the Logarithm Gamma of the Given Number

Akhil Sharma
Updated on 28-Mar-2023 12:20:36

178 Views

In Haskell, we will calculate the logarithm gamma of the given number by using Stirling’s approximation and Lanczos appromixation formula. In the first example, we are going to use Stirling’s approximation with (s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)) function and in the second example, we are going to use Lanczos approximation formula along with (lanczos = log $ ... Read More

Haskell Program to Round a Number to n Decimal Places

Akhil Sharma
Updated on 28-Mar-2023 11:58:06

2K+ Views

In Haskell, we can use round, printf and truncate functions to round a number to n decimal places. In the first example, we are going to use (roundTo n x = (fromInteger $ round $ x * (10^n)) / (10.0^^n)) function and in the second example, we are going to use (roundTo n x = read $ printf ("%." ++ show n ++ "f") x) function.In the third example, we are going to use (roundTo n x = fromIntegral (truncate $ x * 10^n) / 10^n). Algorithm Step 1 − The roundTo function is defined using round function Step ... Read More

How to test the equality of Swift enums with associated values?

Nitin Aggarwal
Updated on 11-Apr-2023 10:41:59

2K+ Views

In Swift, you can use the Equatable protocol to compare enums with associated values. In this article, we will see how we can compare them using the Equatable protocol with an example. Enumeration with Associated Values In Swift, you can provide a value along with an enum case. This makes enumeration more powerful in Swift. A Swift feature called an enumeration with associated values enables you to design a type that can have a finite set of cases, each of which can have a unique set of associated values of any type. This enables you to link data to each ... Read More

How to return the first 5 objects of an array in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:23:43

5K+ Views

In Swift to get the first N objects of an array, we can use the prefix function or Range operator. This prefix function is used to retrieve the prefix elements by passing the count limit. Also, you can use the range operator to get n number of elements from an array. Let's see some examples. In Swift, the prefix function returns an array containing the first n elements. Using the Prefix Function You can use the prefix function to get the first n elements of an array. Step 1 − Create an input array Step 2 − Call the ... Read More

How to iterate for loop in reverse order in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:41:25

6K+ Views

In Swift, there are different approaches to iterating a for loop in reverse order. In this article, you will see some methods like reverse, ranges, stride(from:to:by:), forEach(), etc. Each method can be used in different use cases. Also, you should know that each method has its own performance. Using the reversed() Method This method returns a new array by reversing the elements of an array. It does not change the order of the input array. Step 1 − Create an input array to iterate Step 2 − Perform a for loop with the reversed() function Step 3 − Perform an ... Read More

How to copy text to the clipboard/pasteboard with Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:44:36

3K+ Views

In Swift, there is a dedicated class called UIPasteboard that allows you to copy text to the pasteboard. The same class allows you to paste the text. A UIPasteboard class is part of the UIKit framework that provides a general processor for copying and pasting information in iOS applications. In this class, data can be copied and pasted between apps using a shared instance. You can share various types of information such as text, media files, URLs, and colors. Copy and Paste the Text to the Clipboard Using the UIPasteboard class, you can copy and paste the text value in ... Read More

How to convert float to int in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:45:51

3K+ Views

In Swift, you can convert a float to an Int by using the Int() constructor. This will round the float value toward zero and return an integer value. Remember that, this constructor returns an optional value, so you have to use an optional binding approach to wrap the value safely. In this article, you will see some examples of how to use the Int() constructor. Using the Int() constructor You can use the Int() constructor to convert a Float to an Int. This method rounds the float value toward zero and returns an integer value. Step 1 − Declare ... Read More

How to convert a number to an absolute value in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 11:26:50

3K+ Views

To convert a number to its absolute value in Swift, you can use the abs(_:) function. In this article, you will see multiple examples of how to use the abs() function in the Swift language. Example 1 In this example, you will convert a simple negative number to an absolute value. Step 1 − Declare an input variable with an initial numeric value Step 2 − Convert the numeric input value into absolute value using the abs() function Step 3 − Assign the output absolute value to a new variable Step 4 − Print the input value to the ... Read More

Advertisements