<div dir="ltr">Oh I needed join.<div><br></div><div>computeHead :: (a -> [b]) -> [a] -> [b]</div><div>computeHead f = join . take 1 . fmap f</div><div><br></div><div>That's because</div><div><br></div><div>f :: a -> [b]</div><div><br></div><div>not</div><div><br></div><div>f: a -> b</div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 12, 2015 at 5:22 AM, Martin Vlk <span dir="ltr"><<a href="mailto:martin@vlkk.cz" target="_blank">martin@vlkk.cz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">How about:<br>
<br>
computeHead f = take 1 . fmap f<br>
<br>
Martin<br>
<br>
Dennis Raddle:<br>
<div><div class="h5">> What would be an elegant way of writing this<br>
><br>
> computeHead :: (a -> [b]) -> [a] -> [b]<br>
><br>
> Where when [a] is null, it returns a null list, but when [a] contains one<br>
> or more elements, it applies the given function to the head of a and<br>
> returns that? Is there some existing typeclass operator that facilitates<br>
> this?<br>
><br>
> You can write<br>
><br>
> computeHead _ [] = []<br>
> computeHead f (x:_) = f x<br>
><br>
> But that first line seems suspicious to me... it makes me think about how<br>
> in the list Monad, an empty list falls through. But I can't quite make it<br>
> work.<br>
><br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div></div></div>