Given a list of lists, how to drop the last item in each (sub)list.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

acomber
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 ???
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140102/02343f1f/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

akash g
Hi Angus,

You map the drop function over the list.

Something like this will do

foo xs = map (take 8) xs


On Thu, Jan 2, 2014 at 5:08 PM, 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/18669ed8/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

Peter Hall
In reply to this post by acomber
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>

Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

David Flicker
In reply to this post by acomber
The operation you are trying to is abstracted in Haskell using the
higher-order function *map. *Commonly, we want to take a list or other
sequence of data and perform an operation (function) on each element of the
list. In non-functional languages, we would need to explicitly write a
for-loop through each element (ie
for (int i = 0; i < array.length; i++) {
     array[i] = doSomething(array[i]);
}

Of course in Haskell and other functional languages use of mutable state
and explicit iteration aren't used in most circumstances so new functional
programmers go "Iteration is the same as recursion so I'll write a
recursive function instead something like:
recursiveIteration :: [a] -> [a]
recursiveIteration [] = []
recursiveIteration x:xs = $$ Lots of operations using x $$ :
recursiveIteration xs

Of course, the lazy intermediate functional programmer after writing this
function a few times realizes that "I'm using a functional language, why
not abstract the operations using x using a function and pass the function
as a parameter. Something like this:
recursiveIterationAbstractOperations :: (a -> b) -> [a] -> [b]
recursiveIterationsAbstractOperations _ [] = []
recursiveIterationsAbstractOperations func x:xs = func x :
recursiveIterationsAbstractOperations func xs

This is exactly the definition of *map* in the GHC source
code<http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Base.html#map>
!

I would write it as a map over the outer list and then process each inner
element. Something like this:

discardparitybyte :: [[Bit]] -> [[Bit]]
discardparitybyte = map (take 8)

Learning to use higher-order functions like map, foldl, and filter making
functional programming great. In my mind as an intermediate Haskell
programmer, when I start using direct recursion especially when using
lists, I stop myself and think about a better way to structure my code so
that I can write it as a combination of these and other higher order
functions because they allow for more modulatrity in the code.

-David


On Thu, Jan 2, 2014 at 10:38 PM, 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/da2a8e98/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

akash g
Perhaps a glance at SICP will be helpful.  As David and Peter have rightly
pointed out, higher order functions make it easier to abstract common
patterns.

A glance at the SICP chapter on this will be useful, me thinks.  It helped
me a lot.

<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3">http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3


On Thu, Jan 2, 2014 at 5:44 PM, David Flicker <dtflicker at gmail.com> wrote:

> The operation you are trying to is abstracted in Haskell using the
> higher-order function *map. *Commonly, we want to take a list or other
> sequence of data and perform an operation (function) on each element of the
> list. In non-functional languages, we would need to explicitly write a
> for-loop through each element (ie
> for (int i = 0; i < array.length; i++) {
>      array[i] = doSomething(array[i]);
> }
>
> Of course in Haskell and other functional languages use of mutable state
> and explicit iteration aren't used in most circumstances so new functional
> programmers go "Iteration is the same as recursion so I'll write a
> recursive function instead something like:
> recursiveIteration :: [a] -> [a]
> recursiveIteration [] = []
> recursiveIteration x:xs = $$ Lots of operations using x $$ :
> recursiveIteration xs
>
> Of course, the lazy intermediate functional programmer after writing this
> function a few times realizes that "I'm using a functional language, why
> not abstract the operations using x using a function and pass the function
> as a parameter. Something like this:
> recursiveIterationAbstractOperations :: (a -> b) -> [a] -> [b]
> recursiveIterationsAbstractOperations _ [] = []
> recursiveIterationsAbstractOperations func x:xs = func x :
> recursiveIterationsAbstractOperations func xs
>
> This is exactly the definition of *map* in the GHC source code<http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Base.html#map>
> !
>
> I would write it as a map over the outer list and then process each inner
> element. Something like this:
>
> discardparitybyte :: [[Bit]] -> [[Bit]]
> discardparitybyte = map (take 8)
>
> Learning to use higher-order functions like map, foldl, and filter making
> functional programming great. In my mind as an intermediate Haskell
> programmer, when I start using direct recursion especially when using
> lists, I stop myself and think about a better way to structure my code so
> that I can write it as a combination of these and other higher order
> functions because they allow for more modulatrity in the code.
>
> -David
>
>
> On Thu, Jan 2, 2014 at 10:38 PM, 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
>>
>>
>
> _______________________________________________
> 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/c24f9639/attachment-0001.html>

Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

smekesis@csd.auth.gr
In reply to this post by acomber
Hi Angus! Take a look at the following function:

discardParityByte :: [[Integer]] -> [[Integer]]
discardParityByte = map init

It returns a list constructed by applying "init" to all items in the  
list you pass in. "init" accepts a list and returns the list without  
its last item.

Stavros Mekesis.


Quoting Angus Comber <anguscomber at gmail.com>:

> 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 ???




Reply | Threaded
Open this post in threaded view
|

Given a list of lists, how to drop the last item in each (sub)list.

acomber
I like that :)


On 2 January 2014 15:31, <smekesis at csd.auth.gr> wrote:

> Hi Angus! Take a look at the following function:
>
> discardParityByte :: [[Integer]] -> [[Integer]]
> discardParityByte = map init
>
> It returns a list constructed by applying "init" to all items in the list
> you pass in. "init" accepts a list and returns the list without its last
> item.
>
> Stavros Mekesis.
>
>
>
> Quoting Angus Comber <anguscomber at gmail.com>:
>
>  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/1ef8889b/attachment.html>