Next Continue in Haskell

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Haskell

In Haskell, both next and continue are not keywords. However, there are similar concepts that can provide their functionalities.

next in a loop typically means skipping the current iteration of the loop and moving on to the next iteration. In Haskell, we can achieve this using recursion.

Consider the following example of a function that prints numbers from 1 to 10, but skips number 5:

printNumbers :: Int -> IO()
printNumbers n
  | n > 10 = return ()
  | n == 5 = printNumbers (n + 1)
  | otherwise = do
      print n
      printNumbers (n + 1)

In this code, if n is equal to 5, the function calls itself with n+1, effectively skipping number 5.

continue in a loop typically means skipping the remaining code in the current iteration and moving on to the next iteration. In Haskell, we can achieve this using guard from the Control.Monad library.

Consider the following example of a function that prints only even numbers from 1 to 10:

import Control.Monad

printEvenNumbers :: Int -> IO()
printEvenNumbers n
| n > 10 = return ()
| otherwise = do
guard $ even n
print n
printEvenNumbers (n + 1)

In this code, if n is not an even number, the guard function fails and the rest of the code in the iteration is skipped. The function then moves on to the next iteration.