withinRadius dist point radius (Tree node) = go node []
    where
        sqrRadius = sqr radius
        go node result =
            case node of
                Leaf v a ->
                    let d = point `dist` v
                    in if d < sqrRadius
                       then ((v, a), sqrt d) : result
                       else result
                Node dim dimValue left right ->
                    let (nearChild, farChild) = if point ! dim < dimValue then (left, right) else (right, left)
                        result' = go nearChild result
                    in if sqrAxisDistance dim dimValue point < sqrRadius
                       then go farChild result'
                       else result'
withinRadius dist point radius (Tree node) = go node []
    where
        sqrRadius = sqr radius
        go node result =
            case node of
                Leaf v a ->
                    let d = point `dist` v
                    in if d < sqrRadius
                       then ((v, a), sqrt d) : result
                       else result
                Node dim dimValue left right ->
                    let (nearChild, farChild) = if point ! dim < dimValue then (left, right) else (right, left)
                        result' = go nearChild result
                    in if sqrAxisDistance dim dimValue point < sqrRadius
                       then go farChild result'
                       else result'
withinRadius dist point radius (Tree node) = go node []
    where
        sqrRadius = sqr radius
        go node result =
            case node of
                Leaf v a ->
                    let d = point `dist` v
                    in if d < sqrRadius
                       then ((v, a), sqrt d) : result
                       else result
                Node dim dimValue left right ->
                    let (nearChild, farChild) = if point ! dim < dimValue then (left, right) else (right, left)
                        result' = go nearChild result
                    in if sqrAxisDistance dim dimValue point < sqrRadius
                       then go farChild result'
                       else result'