Monday, November 2, 2015

October 2015 1HaskellADay 1-Liners

  • October 15th, 2015: Matrix-themed problem
    dotProduct :: Num a => [a] -> [a] -> a
    dotProduct a b = sum (zipWith (*) a b)
    point-free-itize the definition
    • Freddy Román @frcepeda dotProduct = sum `dot` zipWith (*) where dot = (.).(.)
  • October 13th, 2015: You're given either fst or snd, but don't know which. Define a function that returns its dual:
    dual :: ((a,a) -> a) -> ((a,a) -> a)
    n.b.: The tuples here have BOTH fst and snd as the same type: a
    Also, a-values have NO typeclass constraints. You CAN NOT use (==) nor (>)
    • Michael Thomas @mjtjunior dual f = f (snd,fst)
    • Francisco Soares Nt @frsoares dual = uncurry . flip . curry
    • Fernando Castor @fernandocastor dual f = \(x,y) -> f (y, x)
      (I hadn't seen @mjtjunior's answer beforehand)
    • Андреев Кирилл @nonaem00 (. Data.Tuple.swap)

No comments: