Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Function not being called specified number of times
open System

let repeat action count =
    for i in 1..count do
        action

let print =
    printfn "Test"

repeat print 10

Results in "Test" being printed only once. How should it be written to print 10 times? If the 10 is currently unused why are there no warnings?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem is that you're not defining print as a function but a constant value.

Functions should take at least one unit parameter:

open System

let repeat action count =
    for i in 1..count do
        action ()

let print () =
    printfn "Test"

repeat print 10

In your original code, the side-effect of printing happens only once, at the definition of print.

And there are no warnings as print it is used, but its value is (), which is the value returned by the printfn "test" function call.

over 4 years ago · Santiago Trujillo Relatório

0

let print =
    printfn "Test"

This is a binding. It means that variable print is assigned to result of execution of printfn "Test". Result of execution of printfnt "Test" is a unit (), which leads to following situation: print equals unit

In your loop you just return unit with each iteration, without actually calling anything.

To get desired results you need to change binding into a function:

open System

let repeat action count =
    for i in 1..count do
        action()

let print() =
    printfn "Test"

repeat print 10
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda