[Haskell-beginners] Using findIndex and then splitAt in Data.List
Geoffrey Bays
charioteer7 at gmail.com
Fri Feb 27 21:41:17 UTC 2015
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 <toad3k at gmail.com> 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 <charioteer7 at gmail.com>
> 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 at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> 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/20150227/c105e342/attachment.html>
More information about the Beginners
mailing list