Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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 <jeffreyjthorn...@gmail.com>
Subject: [Haskell-beginners] Is there an "unscan" function?
To: beginners@haskell.org
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 <orc...@gmail.com>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Jeffrey Thornton <jeffreyjthorn...@gmail.com>
Cc: beginners@haskell.org
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
<jeffreyjthorn...@gmail.com>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
> Beginners@haskell.org
> 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 <dav.vire+hask...@gmail.com>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Kyle Murphy <orc...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <CAM_wFVsDWvK-BmODRz_8y=HLcRkwvOq8m06MoPOe=mqyogn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

2012/1/12 Kyle Murphy <orc...@gmail.com>:
> 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 <dav.vire+hask...@gmail.com>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
To: Kyle Murphy <orc...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <cam_wfvuz3j8iyjrbgktpn9ry953+xxaxx1o6wqazea0gcav...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

2012/1/12 David Virebayre <dav.vire+hask...@gmail.com>:
> 2012/1/12 Kyle Murphy <orc...@gmail.com>:
>> 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 <stephen.tet...@gmail.com>
Subject: Re: [Haskell-beginners] Is there an "unscan" function?
Cc: beginners@haskell.org
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 <orc...@gmail.com> 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
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 43, Issue 15
*****************************************

Reply via email to