Friday, October 30, 2009

More golf...

Update - I submitted it after all, and as a message to self, I really should write emails for public distribution in a separate editor and proof read them properly before pressing send.

Old post begins...

Currently on haskell-beginners there's a thread 'list monad question' with more golf practice. I'd go further than the current pointfree version, but decided not to submit after typing it up. I'm not sure of the merits of posting incomprehensible code on a beginners list.



-- combinations :: Int -> [a] -> [[a]]
-- combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]] .) . replicate

-- The pointfree version can be golfed even further if you consider
-- some useful combinators not found in the standard libraries

combinations :: Int -> [a] -> [[a]]
combinations = foldr (<:>) [[]] `oo` replicate

-- | Applicative 'cons'.

(<:>) :: Applicative f => f a -> f [a] -> f [a]
(<:>) a b = (:) <$> a <*> b



-- | Compose an arity 1 function with an arity 2 function.
--
-- I call this combinator 'specs' (aka glasses) due to its infix
-- appearance `oo`

oo :: (c -> d) -> (a -> b -> c) -> a -> b -> d
oo f g = (f .) . g


Blog Archive

About Me

My photo
Disambiguating biog as there are a few Stephen Tetley's in the world. I'm neither a cage fighter or yachtsman. I studied Fine Art in the nineties (foundation Bradford 1992, degree Cheltenham 1992 - 95) then Computing part-time at Leeds Met graduating in 2003. I'm the Stephen Tetley on Haskell Cafe and Stackoverflow.