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: cleanest way to unwrap a list? (Peter Hall)
2. Re: cleanest way to unwrap a list? (Nick Vanderweit)
3. Re: cabal install errors (damodar kulkarni)
----------------------------------------------------------------------
Message: 1
Date: Tue, 14 Aug 2012 23:59:46 +0100
From: Peter Hall <[email protected]>
Subject: Re: [Haskell-beginners] cleanest way to unwrap a list?
To: "Carlos J. G. Duarte" <[email protected]>
Cc: [email protected]
Message-ID:
<CAA6hAk5ii7q2ptZPOU9ALm0U4X+Ofa_0UcqBPAuk9NKqwy8U=q...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
This is a bit more "functional". The coordinates are the opposite way
around, and it can probably be expressed a bit more concisely, but
I'll leave that to you. This is the basic idea.
-- maps over a list supplying also the 'index'
mapi f = map (uncurry f) . zip [0..]
-- maps over a list supplying also the coordinate pair
mapii f a = mapi f' a
where f' i x = mapi (curry f $ i) x
m0 = [[1,2,3], [4,5,6], [7,8,9]]
f (1,1) a = a+1
f (0,2) a = a-1
f (_,_) a = a
mapii f m0 -- [[1,2,3],[4,6,6],[6,8,9]]
Peter
On 14 August 2012 22:50, Carlos J. G. Duarte
<[email protected]> wrote:
>
> I know it's doable. I was asking if there's a practical / elegant way to do
> it.
> I see a lot of Haskell elegance when the matter is defining math formulas,
> running functions over elements, and so on. But it seems most of that
> elegance goes away when the problem derails a bit.
>
> Now for my problem I come up with this:
>
> modify mat x y f =
> let (lrows, row, rrows) = getpart mat x
> (lcols, col, rcols) = getpart row y
> in lrows ++ [lcols ++ [f col] ++ rcols] ++ rrows
> where
> getpart xs x = let (ls, r:rs) = splitAt x xs in (ls, r, rs)
>
> m0 = [[1,2,3], [4,5,6], [7,8,9]]
>
> main = do
> print m0
> let m1 = modify m0 1 1 succ
> let m2 = modify m1 2 0 pred
> print m2
>
> Which is a bit "awkward" considering the ease it is done in other languages.
>
>
> On 08/14/12 19:35, Tim Perry wrote:
>
> There is a way. Please try to figure it out and if you fail post back with
> your code and we can help you from there.
>
>
>
> On Tue, Aug 14, 2012 at 11:05 AM, Carlos J. G. Duarte
> <[email protected]> wrote:
>>
>> Ok, you all have been showing examples of running functions over elements.
>> Add one, append value, and so on.
>> This works well if there's one or more operations to apply indistinctly to
>> a number of elements.
>>
>> Now, what if we just want to make a single operation to a single element?
>> For example, let's say I have this square matrix
>> [[1,2,3],
>> [4,5,6],
>> [7,8,9]]
>>
>> how can we increment the value 5 (position 2,2) *and* decrement the value
>> 7 (position 3,1)?
>>
>> This is a made up example of course, I just want to see / learn if there's
>> a way to apply a function to a specific subset of elements.
>>
>>
>> On 08/14/12 00:06, Jack Henahan wrote:
>>>
>>> Equally,
>>>
>>> let map' = map . map
>>> map' (+1) . map (++[3]) $ [[1,2],[3,4]]
>>> -- [[2,3,4],[4,5,4]]
>>>
>>> And you can really keep stacking those up. I think this approach will be
>>> cleaner in the long run.
>>>
>>> For instance, let's start naming our parts.
>>> let list = [[1,2],[3,4]]
>>> let map' = map . map
>>> let addOne = map' (+1)
>>> let appendThree = map (++[3])
>>> let reverseInner = map reverse
>>>
>>> So, from here we can do the following:
>>> list
>>> -- [[1,2],[3,4]]
>>>
>>> -- the first example
>>> addOne list
>>> -- [[2,3],[4,5]]
>>> -- now the second example
>>> addOne . appendThree $ list
>>> -- [[2,3,4],[4,5,4]]
>>>
>>> -- now add one to all members of the list, append three to the list,
>>> reverse the inner lists,
>>> -- then add one to all members of the new list
>>>
>>> addOne . reverseInner . appendThree . addOne $ list
>>> -- [[4,4,3],[4,6,5]]
>>>
>>> Now how would you construct that as a list comprehension? With the method
>>> I've proposed, you need
>>> only use map to operate on the nested lists themselves and map' to
>>> operate on the elements of those
>>> lists.
>>>
>>> ====
>>> Jack Henahan
>>> [email protected]
>>>
>>> On Aug 13, 2012, at 6:41 PM, Christopher Howard
>>> <[email protected]> wrote:
>>>
>>>> On 08/12/2012 09:37 PM, Shakthi Kannan wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> --- On Mon, Aug 13, 2012 at 10:51 AM, Christopher Howard
>>>>> <[email protected]> wrote:
>>>>> | Say, for example, I have the list
>>>>> | [[1,2],[3,4]] and want to add 1 to each inner element, resulting in
>>>>> | [[2,3],[4,5]].
>>>>> \--
>>>>>
>>>>> Like this?
>>>>>
>>>>> ghci> let xxs = [[1,2], [3,4]]
>>>>>
>>>>> ghci> [ [ x+1 | x <- xs] | xs <- xxs ]
>>>>> [[2,3],[4,5]]
>>>>>
>>>>> SK
>>>>>
>>>> Thanks everyone for the responses. I found the list comprehension
>>>> approach satisfactory, as it allows me to cleanly modify each layer of
>>>> the nested array as I unwrap it:
>>>>
>>>> code:
>>>> --------
>>>> b = [[ x+1
>>>> | x <- xs ++ [3] ]
>>>> | xs <- [[1,2],[3,4]] ]
>>>>
>>>> *Main> b
>>>> [[2,3,4],[4,5,4]]
>>>> --------
>>>>
>>>> The only downside is that I have to write the layers out in reverse of
>>>> the way I would normally think of them, but that isn't too big of a
>>>> challenge.
>>>>
>>>> I'm not sure how that would be done with map in a way that would be neat
>>>> and readable and wouldn't require declaring extra identifiers. I can't
>>>> give a fair evaluation of the Lens approach because I don't understand
>>>> enough of the theory yet.
>>>>
>>>> --
>>>> frigidcode.com
>>>> indicium.us
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected]
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Tue, 14 Aug 2012 17:02:33 -0600
From: Nick Vanderweit <[email protected]>
Subject: Re: [Haskell-beginners] cleanest way to unwrap a list?
To: [email protected]
Message-ID: <15642346.uGMEUorZhH@euler>
Content-Type: text/plain; charset="us-ascii"
Try to exploit the repeated structure of the list. Here is an implementation
of your "modify" function which does this.
modifyAt :: Int -> (a -> a) -> [a] -> [a]
modifyAt n f xs = let (inits, (e:tails)) = splitAt n xs
in inits ++ (f e):tails
modify :: [[a]] -> Int -> Int -> (a -> a) -> [[a]]
modify mat x y f = modifyAt y (modifyAt x f) mat
Nick
On Tuesday, August 14, 2012 10:50:42 PM Carlos J. G. Duarte wrote:
> I know it's doable. I was asking if there's a practical / elegant way to do
> it. I see a lot of Haskell elegance when the matter is defining math
> formulas, running functions over elements, and so on. But it seems most of
> that elegance goes away when the problem derails a bit.
>
> Now for my problem I come up with this:
> modify mat x y f =
> let (lrows, row, rrows) = getpart mat x
> (lcols, col, rcols) = getpart row y
> in lrows ++ [lcols ++ [f col] ++ rcols] ++ rrows
> where
> getpart xs x = let (ls, r:rs) = splitAt x xs in (ls, r, rs)
>
> m0 = [[1,2,3], [4,5,6], [7,8,9]]
>
> main = do
> print m0
> let m1 = modify m0 1 1 succ
> let m2 = modify m1 2 0 pred
> print m2
> Which is a bit "awkward" considering the ease it is done in other languages.
>
> On 08/14/12 19:35, Tim Perry wrote:
> There is a way. Please try to figure it out and if you fail post back with
> your code and we can help you from there.
>
>
>
> On Tue, Aug 14, 2012 at 11:05 AM, Carlos J. G. Duarte
> <[email protected]> wrote: Ok, you all have been showing examples
> of running functions over elements. Add one, append value, and so on. This
> works well if there's one or more operations to apply indistinctly to a
> number of elements.
>
> Now, what if we just want to make a single operation to a single element?
> For example, let's say I have this square matrix
> [[1,2,3],
> [4,5,6],
> [7,8,9]]
>
> how can we increment the value 5 (position 2,2) *and* decrement the value 7
> (position 3,1)?
>
> This is a made up example of course, I just want to see / learn if there's a
> way to apply a function to a specific subset of elements.
>
>
> On 08/14/12 00:06, Jack Henahan wrote:
> Equally,
>
> let map' = map . map
> map' (+1) . map (++[3]) $ [[1,2],[3,4]]
> -- [[2,3,4],[4,5,4]]
>
> And you can really keep stacking those up. I think this approach will be
> cleaner in the long run.
>
> For instance, let's start naming our parts.
> let list = [[1,2],[3,4]]
> let map' = map . map
> let addOne = map' (+1)
> let appendThree = map (++[3])
> let reverseInner = map reverse
>
> So, from here we can do the following:
> list
> -- [[1,2],[3,4]]
>
> -- the first example
> addOne list
> -- [[2,3],[4,5]]
> -- now the second example
> addOne . appendThree $ list
> -- [[2,3,4],[4,5,4]]
>
> -- now add one to all members of the list, append three to the list,
> reverse the inner lists, -- then add one to all members of the new list
>
> addOne . reverseInner . appendThree . addOne $ list
> -- [[4,4,3],[4,6,5]]
>
> Now how would you construct that as a list comprehension? With the method
> I've proposed, you need only use map to operate on the nested lists
> themselves and map' to operate on the elements of those lists.
>
> ====
> Jack Henahan
> [email protected]
>
> On Aug 13, 2012, at 6:41 PM, Christopher Howard
> <[email protected]> wrote:
>
> On 08/12/2012 09:37 PM, Shakthi Kannan wrote:
> Hi,
>
> --- On Mon, Aug 13, 2012 at 10:51 AM, Christopher Howard
>
> <[email protected]> wrote:
> | Say, for example, I have the list
> | [[1,2],[3,4]] and want to add 1 to each inner element, resulting in
> | [[2,3],[4,5]].
>
> \--
>
> Like this?
>
> ghci> let xxs = [[1,2], [3,4]]
>
> ghci> [ [ x+1 | x <- xs] | xs <- xxs ]
> [[2,3],[4,5]]
>
> SK
>
> Thanks everyone for the responses. I found the list comprehension
> approach satisfactory, as it allows me to cleanly modify each layer of
> the nested array as I unwrap it:
>
> code:
> --------
> b = [[ x+1
> | x <- xs ++ [3] ]
> | xs <- [[1,2],[3,4]] ]
>
> *Main> b
> [[2,3,4],[4,5,4]]
> --------
>
> The only downside is that I have to write the layers out in reverse of
> the way I would normally think of them, but that isn't too big of a
> challenge.
>
> I'm not sure how that would be done with map in a way that would be neat
> and readable and wouldn't require declaring extra identifiers. I can't
> give a fair evaluation of the Lens approach because I don't understand
> enough of the theory yet.
>
> --
> frigidcode.com
> indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Wed, 15 Aug 2012 10:09:39 +0530
From: damodar kulkarni <[email protected]>
Subject: Re: [Haskell-beginners] cabal install errors
To: Benjamin Edwards <[email protected]>
Cc: [email protected]
Message-ID:
<cad5hsyr6fb2ftr9jwswhyoxhzjeoqnsaqys_cv0msjazrgk...@mail.gmail.com>
Content-Type: text/plain; charset="windows-1252"
> Please see this:
> http://ivanmiljenovic.wordpress.com/2010/03/15/repeat-after-me-cabal-is-not-a-package-manager/
>
it is unfortunately true that cabal documentation is very misleading to
many, especially the beginners ...
that's why so many of us need to repeat after-an-expert that
cabal-is-not-a-package-manager ...
But now take a look at how many times the cabal user guide mentions the
term "package" in its documentation, it is very easy to get misled...
Cabal specifies a standard way in which Haskell libraries and applications
> can be *packaged* so that it is easy for consumers to use them, or *
> re-package* them, regardless of the Haskell implementation or
> installation platform.
>
> Cabal defines a common interface ? the *Cabal package* ? between *package
> authors, builders and users*. There is a library to help package authors
> implement this interface, and a tool to enable developers, builders and
> users *to work with Cabal packages*.
>
taken from http://www.haskell.org/cabal/users-guide/
cabal should have been called haskell-make or hmake or something alike...
thanks Benjamin, for the cabal-dev, hsenv tip though.
-Damodar
On Tue, Aug 14, 2012 at 5:38 PM, Benjamin Edwards <[email protected]>wrote:
> I think one point bears repeating: cabal is a build system, really. It
> does a good enough job of that. It is a *terrible* package manager and
> using it as one I think is a classic mistake that the community needs to
> address.
>
> My two-penneth worth is this:
>
> Use cabal-dev, or hsenv, for *everything* and 99% of your woes will go
> away. The the only thing I do when getting haskell up and running is to get
> cabal-dev installed and it's dependencies in the cabal per user pkg store
> and then cabal-dev sandboxes for everything from then on.
> On Aug 14, 2012 11:57 AM, "Carlos J. G. Duarte" <
> [email protected]> wrote:
>
>> On 08/13/12 22:19, Gregory Guthrie wrote:
>>
>> Thanks, I'll try that, but it looks like it could be a lot of maintenance
>> and manual cleanup!
>>
>> I haven't knowingly done any manual upgrades of core packages, but I have
>> done "update"s as asked by cabal when it thinks the database is getting old.
>> I have had such pedestrian usage that I would not have expected to have
>> goofed up the database! :-)
>>
>> Cabal seems to be more troublesome that other various *package managers*
>> like apt, etc...
>>
>>
>> Please see this:
>> http://ivanmiljenovic.wordpress.com/2010/03/15/repeat-after-me-cabal-is-not-a-package-manager/
>>
>> But yes, cabal or not, I agree that there should be a better system for
>> managing haskell packages, like pip, gem or cpan... but that boils down to
>> the problem that some has to do it, and people who are able to do it** are
>> often too busy for that.
>>
>> ** and that doesn't include me, as I'm just starting to explore Haskell
>> on my spare time.
>>
>> All in all, cabal suits me even with its idiosyncrasies.
>>
>>
>> _______________________________________________
>> 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/20120815/024e57ff/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 50, Issue 16
*****************************************