Range Over Iterators in PureScript

To demonstrate iterating over iterators, PureScript provides various features to handle functional programming paradigms. Below is the translation of the provided example, showing how you can create and use iterators in PureScript.

Starting with PureScript, we don’t use structs and methods in the same way. Instead, we utilize records and functions. Here is how you can achieve similar behavior:

Our example will demonstrate iterating over a list and generating Fibonacci numbers using idiomatic PureScript:

module Main where

import Prelude
import Effect (Effect)
import Effect.Console (log)
import Data.Array (fromFoldable)
import Data.Maybe (Maybe(..), fromMaybe)
import Control.Monad.Rec.Class (forever)

-- Define a type alias for List using Maybe to represent the element nodes
type List a = Maybe (Element a)

-- Define the Element type which represents a node in the list
type Element a = 
  { val :: a
  , next :: List a
  }

-- Function to create an empty list
emptyList :: List a
emptyList = Nothing

-- Function to push an element onto the list
push :: forall a. a -> List a -> List a
push x Nothing = Just { val: x, next: Nothing }
push x (Just node) = Just { val: x, next: Just node }

-- Function to convert the List to an Array (for easier iteration)
toArray :: forall a. List a -> Array a
toArray maybeList = fromFoldable (toArrayHelper maybeList)

toArrayHelper :: forall a. List a -> List a
toArrayHelper Nothing = []
toArrayHelper (Just node) = node.val : toArrayHelper node.next

-- Generate a sequence of Fibonacci numbers
genFib :: Array Int
genFib = take 10 (fibHelper 1 1)

-- Helper function for generating Fibonacci sequence
fibHelper :: Int -> Int -> Array Int
fibHelper a b = a : fibHelper b (a + b)

main :: Effect Unit
main = do
  let lst = foldl (flip push) emptyList [10, 13, 23]
  
  -- Iterate over the list and print each element
  log "Iterating over list elements:"
  let elements = toArray lst
  traverse_ logShow elements
  
  -- Collecting all elements into a list and printing them
  log "All elements collected into a list:"
  logShow elements

  -- Using the Fibonacci generator
  log "Fibonacci sequence:"
  let fibSequence = genFib
  traverse_ logShow fibSequence

Explanation

Defining Structures

In PureScript, we use type aliases and records to define our structures. The List type is defined as a Maybe type, which can either be Nothing or a Just containing an Element.

Functions

  • emptyList: Creates an empty list.
  • push: Adds an element to the list.
  • toArray and toArrayHelper: Converts the List to an Array for easier iteration.
  • genFib and fibHelper: Generates a sequence of Fibonacci numbers.

Iteration and Output

We convert the List to an Array to facilitate iteration and use the traverse_ function to print each element. We also show how to collect all elements into a list and print them. Moreover, a Fibonacci sequence generator is demonstrated.

Lastly, we invoke all these operations in the main function to display the results.

Ensure the Code is Executable

You can execute this code in the PureScript environment by compiling it with spago build and running it with spago run. This setup will handle the output and verify the correctness of the operations.