[Haskell-beginners] question about list processing
Alex Hammel
ahammel87 at gmail.com
Thu Nov 12 16:20:28 UTC 2015
With the join it's the same thing as:
computeHead :: (a -> [b]) -> [a] -> [b]
computeHead f xs = take 1 xs >>= f
On Thu, 12 Nov 2015 at 06:58 Dennis Raddle <dennis.raddle at gmail.com> wrote:
> Oh I needed join.
>
> computeHead :: (a -> [b]) -> [a] -> [b]
> computeHead f = join . take 1 . fmap f
>
> That's because
>
> f :: a -> [b]
>
> not
>
> f: a -> b
>
>
> On Thu, Nov 12, 2015 at 5:22 AM, Martin Vlk <martin at vlkk.cz> wrote:
>
>> How about:
>>
>> computeHead f = take 1 . fmap f
>>
>> Martin
>>
>> Dennis Raddle:
>> > What would be an elegant way of writing this
>> >
>> > computeHead :: (a -> [b]) -> [a] -> [b]
>> >
>> > Where when [a] is null, it returns a null list, but when [a] contains
>> one
>> > or more elements, it applies the given function to the head of a and
>> > returns that? Is there some existing typeclass operator that facilitates
>> > this?
>> >
>> > You can write
>> >
>> > computeHead _ [] = []
>> > computeHead f (x:_) = f x
>> >
>> > But that first line seems suspicious to me... it makes me think about
>> how
>> > in the list Monad, an empty list falls through. But I can't quite make
>> it
>> > work.
>> >
>> >
>> >
>> > _______________________________________________
>> > Beginners mailing list
>> > Beginners at haskell.org
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> >
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151112/5880b41c/attachment.html>
More information about the Beginners
mailing list