[Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list.

Peter Hall peter.hall at memorphic.com
Thu Jan 2 12:11:31 UTC 2014


It's usually much simpler and better design to think about the simple case
first and then piece things together. So start with just modifying one
inner list:

   discardparitybyte :: [Bit] -> [Bit]
   discardparitybyte xs = take 9 xs

When you're happy that this works, you can apply it to all the elements of
your data list:

   discardallparitybytes :: [[Bit]] -> [[Bit]]
   discardallparitybytes xs = map discardparitybye xs


And, if you want to, you can make it shorted by making the functions
point-free:

   discardparitybyte = take 9
   discardallparitybytes = map discardparitybye


Peter


On 2 January 2014 11:38, Angus Comber <anguscomber at gmail.com> wrote:

> I have a list like this:
>
>
> [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]]
>
> The 'inner' list is a list of 9 items.  I want to process the list so that
> a list of lists is returned but the 9th element in each inner list is
> dropped.
>
> So the function type would be [[a]] -> [[a]]
>
> So to get started I wrote a function like this:
>
> discardparitybyte :: [[Bit]] -> [[Bit]]
>
> But then not sure how to transform the inner list.
>
> I know I can access the first inner element using take 1 list.  But how do
> I then access/manipulate this inner list?
>
> discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ???
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140102/c2a905a4/attachment.html>


More information about the Beginners mailing list