Tuesday, June 14, 2016

May 2016 1Liners

One-liners
  • May 24th, 2016:
    Given f :: a -> [a] -> b, g :: a -> c
    Write h :: c -> [c] -> b, point-free, in terms of f and g
    where h x y = f (g x) (map g y)
  • May 16th, 2016: The next 3 #1Liner are of a piece, using
    data CmpV a =
       Vec { len :: Int, top :: a,  elts :: [a],
             cmp :: CmpV a -> CmpV a -> Ordering }
    • Give the point-free definition of:
      twoVs :: CmpV a -> CmpV b -> ([a], [b])
    • instance Ord (CmpV a) where
         compare v1 = uncurry (cmp v1) .  (v1,)

      Make compare point-free by removing v1 from above definition
    • An Ord-instance needs an Eq-instance:
      instance Eq a => Eq (CmpV a) where
         v1 == v2 = elts v1 == elts v2

      point-free-itize (==)
  • May 16th, 2016: You have the following lambda:
    \x y -> x == y || fn x y
    where fn :: a -> a -> Bool
    Point-free-itize
    • obadz @obadzz without any fancy uses of the (a ->) Applicative :)
      curry $ foldr (||) False . flip map [(==), fn] . flip uncurry
    • obadz @obadzz with fancy use of (a ->) Applicative :)
      curry $ liftA2 (||) (uncurry (==)) (uncurry fn)
    • Noah Luck Easterly @walkstherain
      curry $ uncurry (||) . (uncurry (==) &&& uncurry fn)
  • May 5th, 2016:
    sames :: Eq a => [a] -> [a] -> Int
    Counts equal values at the same indices in two lists.
    What is the point-free definition?
    • joomy @cattheory
      sames = curry (length . filter (uncurry (==)) . uncurry zip)
    • bazzargh @bazzargh and then Fatih Karakurt @karakfa
      ((length . filter id) .) . zipWith (==)
    • me: sum <<- fromenum="" li="" nbsp="" zipwith="">
    • Noah Luck Easterly @walkstherain
      sames = ((sum . map fromEnum) .) . zipWith (==)
      • `getSum . foldMap (Sum . fromEnum)` seems better than `foldr (bool id succ) 0` but both satisfy `[Bool] -> Int`
    • Андреев Кирилл @nonaem00
      let it = (length .) . (filter id .)  . zipWith (==)

No comments: