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

Geoffrey Bays charioteer7 at gmail.com
Sun Mar 1 02:58:50 UTC 2015


Michael:
Yes, a map with a function that leaves everything I do not want to change
alone, that would indeed be the smooth, obvious FP way to do it. I will
change my code to work this way. Thanks.
Geoffrey
On Feb 28, 2015 10:52 AM, "Michael Orlitzky" <michael at orlitzky.com> wrote:

> 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]
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150228/d2a1d639/attachment.html>


More information about the Beginners mailing list