Skip to content

consider tail-call optimization for mutual recursion #83

Open
@decioferreira

Description

@decioferreira

Consider the following mutually recursive functions:

chompChunks : String -> Int -> Int -> Int -> List Chunk -> List Chunk
chompChunks src pos end start revChunks =
    if pos >= end then
        List.reverse (addSlice start end revChunks)

    else
        let
            word : Char
            word =
                P.unsafeIndex src pos
        in
        case word of
            '\n' ->
                chompEscape src 'n' pos end start revChunks

            '"' ->
                chompEscape src '"' pos end start revChunks

            '\\' ->
                chompEscape src '\\' pos end start revChunks

            {- \r -}
            '\u{000D}' ->
                let
                    newPos : Int
                    newPos =
                        pos + 1
                in
                chompChunks src newPos end newPos (addSlice start pos revChunks)

            _ ->
                let
                    width : Int
                    width =
                        P.getCharWidth word

                    newPos : Int
                    newPos =
                        pos + width
                in
                chompChunks src newPos end start revChunks


chompEscape : String -> Char -> Int -> Int -> Int -> List Chunk -> List Chunk
chompEscape src escape pos end start revChunks =
    let
        pos1 : Int
        pos1 =
            pos + 1
    in
    chompChunks src pos1 end pos1 (Escape escape :: addSlice start pos revChunks)

In this case chompEscape is not tail-call optimized, because it calls chompChunks with a different set of arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions