Skip to content

Instantly share code, notes, and snippets.

@corajr
Last active August 29, 2015 14:28
Show Gist options
  • Select an option

  • Save corajr/7d2eff4dbcff6a7df4a6 to your computer and use it in GitHub Desktop.

Select an option

Save corajr/7d2eff4dbcff6a7df4a6 to your computer and use it in GitHub Desktop.
Two ways to filter a list in Haskell
<p>What do two different ways of filtering a list compile to?</p>
<p>(Example: take a list of tuples (a,b), and return a list of a for every (a,b) where b > 1.)</p>
<table>
<tr>
<th>
`map fst . filter ((>1) . snd)`
</th>
<th>
`[a | (a,b) <- x, b > 1]`
</th>
</tr>
<tr>
<td valign="top">
<pre>
Rec {
Main.f1_go [Occ=LoopBreaker] :: [(Int, Int)] -> [Int]
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U>]
Main.f1_go =
\ (ds_a164 :: [(Int, Int)]) ->
case ds_a164 of _ [Occ=Dead] {
[] -> GHC.Types.[] @ Int;
: y_a169 ys_a16a ->
case y_a169 of _ [Occ=Dead] { (ds1_a16v, y1_a16w) ->
case y1_a16w of _ [Occ=Dead] { GHC.Types.I# x_a173 ->
case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.># x_a173 1)
of _ [Occ=Dead] {
False -> Main.f1_go ys_a16a;
True -> GHC.Types.: @ Int ds1_a16v (Main.f1_go ys_a16a)
}
}
}
}
end Rec }
</pre>
</td>
<td valign="top">
<pre>
Rec {
Main.f2_go [Occ=LoopBreaker] :: [(Int, Int)] -> [Int]
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U>]
Main.f2_go =
\ (ds_a164 :: [(Int, Int)]) ->
case ds_a164 of _ [Occ=Dead] {
[] -> GHC.Types.[] @ Int;
: y_a169 ys_a16a ->
case y_a169 of _ [Occ=Dead] { (a_aEq, b_aEr) ->
case b_aEr of _ [Occ=Dead] { GHC.Types.I# x_a173 ->
case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.># x_a173 1)
of _ [Occ=Dead] {
False -> Main.f2_go ys_a16a;
True -> GHC.Types.: @ Int a_aEq (Main.f2_go ys_a16a)
}
}
}
end Rec }
</pre>
</td>
</tr>
</table>

What do two different ways of filtering a list compile to?

(Example: take a list of tuples (a,b), and return a list of a for every (a,b) where b > 1.)

`map fst . filter ((>1) . snd)` `[a | (a,b) <- x, b > 1]`
Rec {
Main.f3 [Occ=LoopBreaker]
  :: forall a_aWS. [(a_aWS, Integer)] -> [a_aWS]
Main.f3 =
  \ (@ a_aWS) (ds_a16M :: [(a_aWS, Integer)]) ->
    case ds_a16M of _ [Occ=Dead] {
      [] -> GHC.Types.[] @ a_aWS;
      : y_a16R ys_a16S ->
        case y_a16R of _ [Occ=Dead] { (ds1_a17j, y1_a17k) ->
        case integer-gmp-1.0.0.0:GHC.Integer.Type.gtInteger#
               y1_a17k Main.f5
        of wild2_a18o { __DEFAULT ->
        case GHC.Prim.tagToEnum# @ Bool wild2_a18o of _ [Occ=Dead] {
          False -> Main.f3 @ a_aWS ys_a16S;
          True -> GHC.Types.: @ a_aWS ds1_a17j (Main.f3 @ a_aWS ys_a16S)
        }
        }
        }
    }
end Rec }
f2
  :: forall t_aVS a_aVT.
     (Num a_aVT, Ord a_aVT) =>
     [(t_aVS, a_aVT)] -> [t_aVS]
f2 =
  \ (@ t_aVX)
    (@ a_aVY)
    ($dNum_aVZ :: Num a_aVY)
    ($dOrd_aW0 :: Ord a_aVY)
    (x_aE5 :: [(t_aVX, a_aVY)]) ->
    let {
      lvl_s19c :: a_aVY
      [LclId, Str=DmdType]
      lvl_s19c = fromInteger @ a_aVY $dNum_aVZ Main.f5 } in
    letrec {
      go2_a16L [Occ=LoopBreaker] :: [(t_aVX, a_aVY)] -> [t_aVX]
      [LclId, Arity=1, Str=DmdType ]
      go2_a16L =
        \ (ds_a16M :: [(t_aVX, a_aVY)]) ->
          case ds_a16M of _ [Occ=Dead] {
            [] -> GHC.Types.[] @ t_aVX;
            : y_a16R ys_a16S ->
              case y_a16R of _ [Occ=Dead] { (a1_aE6, b_aE7) ->
              case > @ a_aVY $dOrd_aW0 b_aE7 lvl_s19c of _ [Occ=Dead] {
                False -> go2_a16L ys_a16S;
                True -> GHC.Types.: @ t_aVX a1_aE6 (go2_a16L ys_a16S)
              }
              }
          }; } in
    go2_a16L x_aE5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment