Tuesday, October 3, 2017

September 2017 1HaskellADay 1Liner problems and solutions

  • September 26th, 2017:
    art2RawNames art = Raw (artId art) <$> (Map.lookup "Person" $ metadata art)
    curry away the 'art' var. ref: Y2017.M09.D26.Exercise 
  • September 19th, 2017:  The #1Liner to follow (next tweet) is a question based off a code snippet from Y2017.M09.D18.Solution. What is a better/more elegant definition?
    fmap (wordStrength . wc) (BL.readFile filename) >>= \dict ->
    return (Map.insert filename dict m)
    Reformulate. Curry the dict-ref.

Saturday, September 30, 2017

September 2017 1HaskellADay problems and solutions

Tuesday, September 19, 2017

August 2017 1HaskellADay 1Liners Problems and Solutions

  • August 1st, 2017:
  • f :: (Maybe a, b) -> Maybe (a, b) Define points-free.
  • August 1st, 2017:
    Given f above and f a and f b are mutually exclusive in Maybe monad, define
    g :: Maybe (a, b) -> Maybe (a, b) -> (Maybe a, b)
    points free
  • August 1st, 2017:
    Now, let's define the dual of f
    f' :: Maybe (a, b) -> (Maybe a, b)
    points free

Friday, September 1, 2017

August 2017 1HaskellADay problems and solutions

Tuesday, August 1, 2017

July 2017 1HaskellADay 1Liner

  • July 7th, 2017:
    In LU-decomposition of matrices you have square P-matrix:
    [[1,0..],
     [0,2,0..],
     [0,0,3,0..],
    ...]
    For matrices of n² size
    Code that
    • ∃! David Turner @DaveCTurner
      • matrix n = let td = take n . drop 1 in td [td $ replicate i 0 ++ [i] ++ repeat 0 | i <- [0..]]

Monday, July 31, 2017

July 2017 1HaskellADay Problems and Solutions

Friday, July 7, 2017

June 2017 1HaskellADay 1Liners

  • June 17th, 2017:
    f :: (a, [a]) -> [a] -> [a]
    f (c, w1) w2 = c:w1 ++ w2

    Define f points-free
    • bazzargh @bazzargh (++).uncurry(:)
      • Felt there must be a nicer way to exploit symmetry of mappend.uncurry(mappend.pure) but can't find it

Monday, July 3, 2017

June 2017 1HaskellADay Problems and Solutions

Friday, June 16, 2017

May 2017 1Liners 1HaskellADay

  • May 10th, 2017:
    Define (^) :: (a -> a) -> Int -> (a -> a)
    The 'power'-function where f ^ 3 = f . f . f
    • Conor McBride @pigworker flip ((ala Endo foldMap .) . replicate)

Wednesday, June 7, 2017

May 2017 1HaskellADay problems and solutions

Wednesday, May 10, 2017

April 2017 1HaskellADay 1Liners

  • April 14th, 2017: given
    eitherOr, neitherNor :: Eq a => a -> a -> a -> Bool

    Is eitherOr not neitherNor?

    Prove or disprove.
  • April 14th, 2017: given
    neitherNor :: Eq a => a -> a -> a -> Bool
    andNot :: Eq a => a -> a -> Bool

    How do you compose neitherNor 1 0 and andNot 4?
  • April 11th, 2017:

    opts :: Credentials -> Options
    opts c = defaults & auth ?~ basicAuth (pack $ keyToken c) (pack $ secretToken c)

    point-free-itize

    given:

    data Credentials = Credentials { keyToken, secretToken :: String }

    and (?~) and (&) are from Control.Lens

    Snaps for elegance

    The above code from quillio/Twillo.hs by ismailmustafa

Monday, May 1, 2017

April 2017 1HaskellADay Problems and Solutions

Tuesday, April 11, 2017

March 2017 1HaskellADay 1Liners

  • March 30th, 2017:
    divide :: (a -> Either b c) -> [a] -> ([b], [c])  This function is somewhere easy to get to, right?
    via @fmapE 
    • @fmapE: divide f = foldr (either (first . (:)) (second . (:)) . f) ([], []) 
    • SocialJusticeCleric @walkstherain divide f = Data.Either.partitionEithers . fmap f
    • andrus @andrus divide f = foldMap $ (swap . pure . pure ||| pure . pure) . f
    • matt @themattchan divide = partitionEithers ... map where ... = (.).(.)
  • March 13th, 2017:
    Palindromes have nothing to do with today's #haskell problem of anagrams. So what.

    Define
    palindrome :: String -> Bool
    elegantly
    • SocialJusticeCleric @walkstherain all id . (zipWith (==) =<< reverse)
      • SocialJusticeCleric added: TIL Data.Foldable.and
    • bazzargh @bazzargh ap (==) reverse?
  • March 13th, 2017:
    type Bag a = Map a Int

    anagram :: Ord a => [a] -> [a] -> Bool
    anagram src = (== Bag.fromList src) . Bag.fromList

    Redefine with <*>
    • Denis Stoyanov @xgrommx why not?
      anagram = (==) `on` Bag.fromList

Wednesday, April 5, 2017

March 2017 1HaskellADay Problems and Solutions

Monday, March 13, 2017

January 2017 1HaskellADay 1Liners

  • January 31st, 2017:
    You have d = "3.461497957769017000D+07"
    define parseDouble :: String -> Double
    (n.b.: (read d) :: Double throws an error)
    • bazzargh @bazzargh uncurry (*) (bimap read ((10^).read.(dropWhile(`elem`"0+D"))) (break (=='D') d))::Double
  • January 31st, 2017: Given
    e :: FilePath -> [String] -> [Epoch]

    readEpochs :: FilePath -> IO [Epoch]
    readEpochs f = e f . lines <$> readFile f

    point-free-itize readEpochs
    • Astynax Pirogov @alex_pir uncurry fmap . (((. lines) . e) &&& readFile
  • January 30th, 2017: Given

    parseNum :: String -> Maybe (Float, String)

    define: dropNum'' :: String -> Maybe String

    points-free in terms of parseNum
    • matt @themattchan dropNum" = fmap snd . parseNum
  • January 30th, 2017: For

    parseHeader :: String -> Maybe String
    parseHeader str = match "Start " str <|> match "Finish " str

    eliminate redundancies
    • mconcat . ([match] <*> ["Start ", "Finish "] <*>) . pure
    • Nickolay Kudasov @crazy_fizruk If you're allowed to use Monoid instead of Alternative, how about this version?
      foldMap match ["Start", "Finish"]
    • Andreas Källberg @Anka213  My solution was
      parseHeader str = foldr1 (<|>) . map (`match` str) $ ["Start", "Finish"]
      But that's longer than the original.
  • January 25th, 2017:
    given f is type: f :: Int -> a -> Bool

    for: g :: a -> Bool
    g = (||) . f 2 <*> f 27

    rewrite g using f only once in the definition
    • Denis Stoyanov @xgrommx ugly version but
      (liftA2 . liftA2) (||) ($2) ($27) f
  • January 19th, 2017:
    import Data.Tree.Merkle

    mkleaf :: Show a => a -> Leaf a
    mkleaf = uncurry Leaf . (show . hashDatum &&& id)

    redefine using (<*>)
    • Denis Stoyanov @xgrommx smth like
      mkleaf = uncurry Leaf . show . (hashDatum <$> (,) <*> id)
      mkleaf = uncurry Leaf . show . (liftA2 (,) hashDatum id)
  • January 19th, 2017:
    mkbranch1 :: Leaf a -> Branch a
    mkbranch1 = uncurry Twig . (uncurry childrenHash . (dataHash &&& dataHash) &&& id)

    redefine using (<*>)s(?)

Tuesday, February 28, 2017

February 2017 1HaskellADay Problems and Solutions

Tuesday, January 31, 2017

January 2017 1HaskellADay problems and solutions