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

akash g akaberto at gmail.com
Thu Jan 2 12:19:17 UTC 2014


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.

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>


More information about the Beginners mailing list