[Haskell-beginners] Using findIndex and then splitAt in Data.List

Michael Orlitzky michael at orlitzky.com
Sat Feb 28 15:52:17 UTC 2015


On 02/28/2015 10:02 AM, Geoffrey Bays wrote:
> Michael:
> Thanks, split on would work nicely.
> 
> I probably should have backed up in my reasoning process to say that my
> real task is to replace a data item in a list with another altered data
> item in the same position, or rather in FP, create a new list with the
> altered item in the same place. And I need a predicate to find the name of
> the item I want to replace.
> Any thoughts?
> 


You can define a function to "fix" the thing that you want to alter, and
then map that function over the list. Just make sure that the function
only alters the thing that you want to alter. For example, suppose the
other numbers are afraid of 7, so you want to change 7 into 8 in some list:

  -- Turn 7 into 8; leave everything else alone.
  fix_seven :: Int -> Int
  fix_seven 7 = 8
  fix_seven x = x

  numbers :: [Int]
  numbers = [1..10]

  ghci> print $ map fix_seven numbers
  [1,2,3,4,5,6,8,8,9,10]



More information about the Beginners mailing list