Using findIndex and then splitAt in Data.List

Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int? This is in an IO() do Like so: (This is in an IO() do block) let gradeItemIndex = findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList let twoListsTuple = splitAt gradeItemIndex gradeItemList // does not compile obviously Many Thanks, Geoffrey

You will probably do a case match on the result of findIndex to find out
whether you want to split there. You will have to deal with the
possibility that the item you are searching for is not in the list.
Untested:
let twoListsTuple = case findIndex (\g -> (itemName g) == (itemName
gradeItemP)) gradeItemList of
Nothing -> (gradeItemList,[])
Just newlists -> newlists
On Fri, Feb 27, 2015 at 3:51 PM, Geoffrey Bays
Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int? This is in an IO() do
Like so: (This is in an IO() do block)
let gradeItemIndex = findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList let twoListsTuple = splitAt gradeItemIndex gradeItemList
// does not compile obviously
Many Thanks,
Geoffrey
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

David:
Thanks for putting me on the right track. This works:
let twoListsTuple = case findIndex (\g -> (itemName g) == (itemName
gradeItemP)) gradeItemList of
Nothing -> (gradeItemList, [])
Just x -> splitAt x gradeItemList
I was not sure if the splitAt function would take a Just Int in place of a
regular Int without complaint, but that seems to be the case.
As it turns out, I can be certain from the context that the item will
definitely be found in the list, but the case statement allows for the
unpacking of the Maybe.
Thanks,
Geoffrey
On Fri, Feb 27, 2015 at 4:16 PM, David McBride
You will probably do a case match on the result of findIndex to find out whether you want to split there. You will have to deal with the possibility that the item you are searching for is not in the list. Untested:
let twoListsTuple = case findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList of Nothing -> (gradeItemList,[]) Just newlists -> newlists
On Fri, Feb 27, 2015 at 3:51 PM, Geoffrey Bays
wrote: Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int? This is in an IO() do
Like so: (This is in an IO() do block)
let gradeItemIndex = findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList let twoListsTuple = splitAt gradeItemIndex gradeItemList
// does not compile obviously
Many Thanks,
Geoffrey
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Ah yeah, I messed up a bit. Another way you can deal with this is using
the maybe function. If you are absolutely positive that it can never fail,
it doesn't hurt to choose a default index of 0.
let gradeItemIndex = ...
let twoListsTuple = splitAt (maybe 0 id gradeItemIndex)
However if the code around it changes your assumption may become
invalidated in the future, and your code will explode.
On Fri, Feb 27, 2015 at 4:41 PM, Geoffrey Bays
David: Thanks for putting me on the right track. This works:
let twoListsTuple = case findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList of Nothing -> (gradeItemList, []) Just x -> splitAt x gradeItemList
I was not sure if the splitAt function would take a Just Int in place of a regular Int without complaint, but that seems to be the case. As it turns out, I can be certain from the context that the item will definitely be found in the list, but the case statement allows for the unpacking of the Maybe.
Thanks,
Geoffrey
On Fri, Feb 27, 2015 at 4:16 PM, David McBride
wrote: You will probably do a case match on the result of findIndex to find out whether you want to split there. You will have to deal with the possibility that the item you are searching for is not in the list. Untested:
let twoListsTuple = case findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList of Nothing -> (gradeItemList,[]) Just newlists -> newlists
On Fri, Feb 27, 2015 at 3:51 PM, Geoffrey Bays
wrote: Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int? This is in an IO() do
Like so: (This is in an IO() do block)
let gradeItemIndex = findIndex (\g -> (itemName g) == (itemName gradeItemP)) gradeItemList let twoListsTuple = splitAt gradeItemIndex gradeItemList
// does not compile obviously
Many Thanks,
Geoffrey
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Fri, Feb 27, 2015 at 04:51:46PM -0500, David McBride wrote:
Another way you can deal with this is using the maybe function. If you are absolutely positive that it can never fail, it doesn't hurt to choose a default index of 0.
let gradeItemIndex = ... let twoListsTuple = splitAt (maybe 0 id gradeItemIndex)
And if you don't trust your gut feelings, instead of `maybe 0...` place maybe (error "tlt") id gradeItemIndex as some kind of crude assertion.

On 02/27/2015 03:51 PM, Geoffrey Bays wrote:
Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int?
Once you understand why you can't feed a (Maybe Int) to a function that takes an Int, throw it all away and use `splitOn` from the "split" package instead =) https://hackage.haskell.org/package/split-0.2.2/docs/Data-List-Split.html

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?
Thanks,
Geoffrey
On Feb 28, 2015 9:08 AM, "Michael Orlitzky"
On 02/27/2015 03:51 PM, Geoffrey Bays wrote:
Hi. An elementary question here about two functions in Data.List: how to use a value from findIndex which returns a Maybe Int, and then use that result in splitAt which takes a regular Int?
Once you understand why you can't feed a (Maybe Int) to a function that takes an Int, throw it all away and use `splitOn` from the "split" package instead =)
https://hackage.haskell.org/package/split-0.2.2/docs/Data-List-Split.html
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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]

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"
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (4)
-
David McBride
-
Francesco Ariis
-
Geoffrey Bays
-
Michael Orlitzky