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. Is there an "unscan" function? (Jeffrey Thornton)
2. Re: Is there an "unscan" function? (Kyle Murphy)
3. Re: Is there an "unscan" function? (David Virebayre)
4. Re: Is there an "unscan" function? (David Virebayre)
5. Re: Is there an "unscan" function? (Stephen Tetley)
----------------------------------------------------------------------
Message: 1
Date: Wed, 11 Jan 2012 22:44:51 -0600
From: Jeffrey Thornton <[email protected]>
Subject: [Haskell-beginners] Is there an "unscan" function?
To: [email protected]
Message-ID:
<CAPXF-xLrL3xj_haALxFKGjwmwJams=08wbe3so8t4ppqxwj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hello-
Is there standard function in Haskell that effectively does an inverse
scan? For example,
scanl1 (\ x y -> x+y) [1,2,3,4] == [1,3,6,10].
So is there a very simple built-in way to do this hypothetical example?:
unscanl1 (\ x y -> y-x) [1,3,6,10] == [1,2,3,4]
Thanks,
Jeffrey
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120111/1b26e2ea/attachment-0001.htm>
------------------------------
Message: 2
Date: Thu, 12 Jan 2012 01:41:46 -0500
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Jeffrey Thornton <[email protected]>
Cc: [email protected]
Message-ID:
<CA+y6JcwTG6-e3ttv9JO+XMu4ZP0X=ejooa_3db982npu7m9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I was able to build something incredibly convoluted that accomplishes what
you want, but I'm sure there's a better way to do it.
unscanl1 :: (a -> a -> a) -> [a] -> [a]
unscanl1 f xs = (head xs) : (map (\(a:b:_) -> f a b) $ convert xs)
where
convert = takeWhile (\xs -> length xs == 2) . map (take 2) . tails
I'm also not sure if this works in the general case, but it worked with the
example you gave and a couple other quick test cases I thought up. As with
any case where you use head, bad stuff will happen if feed an empty list,
so either add a case that matches on [] or make sure not to feed it an
empty list.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Wed, Jan 11, 2012 at 23:44, Jeffrey Thornton
<[email protected]>wrote:
> Hello-
>
> Is there standard function in Haskell that effectively does an inverse
> scan? For example,
>
> scanl1 (\ x y -> x+y) [1,2,3,4] == [1,3,6,10].
>
> So is there a very simple built-in way to do this hypothetical example?:
>
> unscanl1 (\ x y -> y-x) [1,3,6,10] == [1,2,3,4]
>
> Thanks,
> Jeffrey
> _______________________________________________
> 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/20120112/55e75579/attachment-0001.htm>
------------------------------
Message: 3
Date: Thu, 12 Jan 2012 08:17:45 +0100
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Kyle Murphy <[email protected]>
Cc: [email protected]
Message-ID:
<CAM_wFVsDWvK-BmODRz_8y=HLcRkwvOq8m06MoPOe=mqyogn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
2012/1/12 Kyle Murphy <[email protected]>:
> I was able to build something incredibly convoluted that accomplishes what
> you want, but I'm sure there's a better way to do it.
Not sure if it's a better way.
unscan l = head l : zipWith (subtract) l (tail l)
Corrected to work for empty lists :
unscan [] = []
unscan l@(x:xs) = x : zipWith (subtract) l xs
David.
------------------------------
Message: 4
Date: Thu, 12 Jan 2012 08:22:13 +0100
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Kyle Murphy <[email protected]>
Cc: [email protected]
Message-ID:
<cam_wfvuz3j8iyjrbgktpn9ry953+xxaxx1o6wqazea0gcav...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
2012/1/12 David Virebayre <[email protected]>:
> 2012/1/12 Kyle Murphy <[email protected]>:
>> I was able to build something incredibly convoluted that accomplishes what
>> you want, but I'm sure there's a better way to do it.
>
> Not sure if it's a better way.
Disregard that reply, I overlooked the question. My only excuse is
that it's early in the morning.
that would be :
unscan f l = if null l then [] else head l : zipWith f l (tail l)
David. Where's my coffee ! Silly me. I don't drink coffee.
------------------------------
Message: 5
Date: Thu, 12 Jan 2012 07:37:15 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
Cc: [email protected]
Message-ID:
<cab2tprcub5zpsh+trhq1+umugyknneayz0kjqerzp5cxkrv...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Direct recursion is almost always clearer if you are traversing the
list at a "different speed". The usual list functionals (map, filter,
folds) are all speed 1 - traversing one element at a time. Here we
want pairwise traversal:
unscan :: (a -> a -> b) -> [a] -> [b]
unscan f (a:b:bs) = f a b : unscan f b bs
unscan _ _ = []
On 12 January 2012 06:41, Kyle Murphy <[email protected]> wrote:
> I was able to build something incredibly convoluted that accomplishes what
> you want, but I'm sure there's a better way to do it.
>
...
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 43, Issue 15
*****************************************