Iteration using counters in functional programming?

Babra Cunningham

I'm iterating through a list of [Char], I'm wondering the best way to perform the iteration.

This function calls itself recursively and passes back to itself a counter that gets decreased per iteration.

Its just for illustrative purposes, so always return True for each Char:

text = ["abcsdsdsdsd"]

exampleFunction :: Int -> String -> [Bool]
exampleFunction textlen text
  | textlen >= 0        = True : exampleFunction (textlen - 1) text
  | otherwise   = []

Using 'textlen' as a counter seems rather imperative, is this the best way and/or the most Haskell-like to implement this?

chepner

The counter isn't necessary. The length of the list isn't particularly important; you just use it to determine if the list is non-empty (textlen >= 0) or not. You can do the same thing by pattern matching on the argument itself.

exampleFunction :: [String] -> [Bool]
exampleFunction (x:xs) = True : exampleFunction xs
exampleFunction []  = []

As pointed out in a comment, this particular recursion scheme is abstracted out as the map function:

exampleFunction :: [String] -> [Bool]
exampleFunction xs = map (const True) xs

where const True returns a function that returns True no matter what its argument is. (const True can also be written as an explicit lambda expression (\_ -> True). map applies this function to each element of xs and combines the results into a new list.

Other recursion schemes capture different styles of recursion (filter, foldr, etc), so while it is always possible to write an explicit recursive function to accomplish your task, you can often avoid it by using the appropriate higher order function.


I'm abusing the term recursion scheme a bit; map and filter can be thought of as special cases of foldr, which is an example of a general recursion scheme known as a catamorphism.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Counters in functional programming

find() using Functional Programming

Using functional programming with ggplot

Functional programming functions using 2 lists in D

Avoid using set! into functional programming style algorithm

Unique array of random numbers using functional programming

Count occurrences of each tag using functional programming

How to filter to multiple paths using functional programming

How to properly replace 'extends', using functional programming?

Does Using Caching impove the performance of functional programming?

Recursion to iteration conversion using dynamic programming

Iterating over (x,y) positions of a grid using functional programming methods

Simplifying the if-else condition using Java Functional Style Programming

Java: How to properly manipulate BigDecimal array using functional programming?

Using functional programming, Extract the superclass fields providing subclass object

How to rewrite this code in Scala using a Functional Programming approach

Generic method as functional programming using java.util.Function

Scala way of merging tuples in a list using functional programming techniques

Can functional reactive programming (FRP) be expressed using monads?

Convert nested array of strings into nested array of doubles using functional programming

Functional Programming in Kotlin: Counting elements in list by using fold

Averaging elements in array of arrays by index using functional programming

Using functional programming to parse opening hours from Google Places api

Mapping arrays in Python using map, lambda, and functional programming workflows

Stream and Filter using Functional Programming for Server object in List<Server>

How to manage DB connection in Scala using functional programming style?

what is the advantage of using functional programming in this example? I have used JavaScript

How to append data in Functional programming style using kotlin with arrow library

How can I get started with functional programming using react?