Skip to content

This kind of code compiles and runs extremely slow.  #12698

@RickyYCheng

Description

@RickyYCheng

I've learned a "skill" due to this question.
codewars: A Simple Postfix Language
And my answer is:

let begin_ f = f []
let push s n f = f (n :: s)
let add (a :: b :: s) cont = cont ((a + b) :: s)
let end_ = List.head

then we can call begin_ push 1 push 2 add end_ in F#!

In Haskell version, they advise:

 The inferred type of a basic implementation is exponential in size to the expression. This causes type inference to take too long.
 Hint: Use a typeclass
 Hint: http://chrisdone.com/posts/haskell-constraint-trick

for this kind of code, the F# version will be slow too.

bad = begin push 1 push 1 push 1 push 1 push 1 push 1 push 1 push 1 push 1
        add add add add add add add add end

my quetsion is why do our program will run slowly?
and if you just use the pipeline |> such as

let begin_ = [] 
let push s n = s::n 
let add (a::b::s) = (a+b)::s
let end_ = List.head

it will have a very beautiful performance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions