Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using List Comprehension in place of Recursion

Tags:

haskell

I am curious whether or not using list comprehension in place of recursion is possible for the following example.

The function replaceFirst that takes in an an element and a list and replaces the first occurrence of the element from the list.

This can be done using recursion as follows:

replaceFirst _ [] = []
replaceFirst elem y (x:xs) | x==y = (elem:xs)
                           | otherwise = x:replaceFirst elem y xs

My question is, can this recursive function, or a similar recursive function that operates on the first occurrence of an element in a list, be replaced with a list comprehension function? Why or why not? (I am more concerned with the reasoning than the actual code).

like image 603
AnchovyLegend Avatar asked Feb 18 '23 10:02

AnchovyLegend


1 Answers

List comprehensions are syntactic sugar for various forms of map, filter, and concatMap. If your recursive function can be described in terms of these, then you can rewrite it to a list comprehension. They can't short circuit, as you do above, nor can they pass accumulating state.

Your replaceFirst would seem to require an accumulator to "tell" later elements in the list about the appearance of earlier elements. I think this makes it difficult or impossible to write using only list comprehension syntax.

like image 199
Don Stewart Avatar answered Mar 11 '23 09:03

Don Stewart