Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Given a list of lists, how to drop the last item in each
(sub)list. (akash g)
2. Re: Given a list of lists, how to drop the last item in each
(sub)list. (Peter Hall)
3. Re: Given a list of lists, how to drop the last item in each
(sub)list. (David Flicker)
4. Re: Given a list of lists, how to drop the last item in each
(sub)list. (akash g)
----------------------------------------------------------------------
Message: 1
Date: Thu, 2 Jan 2014 17:32:42 +0530
From: akash g <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Given a list of lists, how to drop
the last item in each (sub)list.
Message-ID:
<caliga_daog+5bezpmraykrxcwlxwbght42gc_olrxkp4cmj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 2
Date: Thu, 2 Jan 2014 12:11:31 +0000
From: Peter Hall <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Given a list of lists, how to drop
the last item in each (sub)list.
Message-ID:
<CAA6hAk5G560H7jct9Ltw1kp70D=nsvroOP5pNw3++9=qrmx...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 3
Date: Thu, 2 Jan 2014 23:14:20 +1100
From: David Flicker <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Given a list of lists, how to drop
the last item in each (sub)list.
Message-ID:
<CABTOPC3AD=aqgtkctbdq_gn6b5bfmhqdapnh1r5a3van53x...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 4
Date: Thu, 2 Jan 2014 17:49:17 +0530
From: akash g <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Given a list of lists, how to drop
the last item in each (sub)list.
Message-ID:
<caliga_dw_1bk4ajtxywdr4orwci9rjpa2h4go3aesh04akv...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]> 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 <[email protected]>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
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 67, Issue 3
****************************************