Usando funções no F#

Uma definição de função simples é semelhante à seguinte:

let f x = x + 1

No exemplo anterior, o nome da função é f, o argumento é x, que tem o tipo int, o corpo da função é x + 1 e o valor de retorno é do tipo int.

Uma característica definidora do F# é que as funções têm status de primeira classe. Você pode fazer com uma função o que pode fazer com valores dos outros tipos internos, com um grau de esforço comparável.

  • Você pode dar nomes de valores à função.

  • Você pode armazenar funções em estruturas de dados, como em uma lista.

  • Você pode passar uma função como um argumento em uma chamada de função.

  • Você pode retornar uma função de uma chamada de função.

Dar um nome ao valor

Se uma função for um valor de primeira classe, você deverá ser capaz de nomeá-la, assim como pode nomear inteiros, cadeias de caracteres e outros tipos internos. Isso é conhecido na literatura de programação funcional como associação de um identificador a um valor. O F# usa associações let para associar nomes a valores: let <identifier> = <value>. O código a seguir mostra dois exemplos.

// Integer and string.
let num = 10
let str = "F#"

Você pode nomear uma função com a mesma facilidade. O exemplo a seguir define uma função nomeada squareIt associando o identificador squareIt à expressão lambdafun n -> n * n. A função squareIt tem um parâmetro n e retorna o quadrado desse parâmetro.

let squareIt = fun n -> n * n

O F# fornece a sintaxe mais concisa a seguir para obter o mesmo resultado com menos digitação.

let squareIt2 n = n * n

Os exemplos a seguir usam principalmente o primeiro estilo let <function-name> = <lambda-expression> para enfatizar as semelhanças entre a declaração de funções e a declaração de outros tipos de valores. No entanto, todas as funções nomeadas também podem ser gravadas com a sintaxe concisa. Alguns dos exemplos são gravados de ambas as maneiras.

Armazenar o valor em uma estrutura de dados

Um valor de primeira classe pode ser armazenado em uma estrutura de dados. O código a seguir mostra exemplos que armazenam valores em listas e tuplas.

// Lists.

// Storing integers and strings.
let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]
let stringList = [ "one"; "two"; "three" ]

// You cannot mix types in a list. The following declaration causes a
// type-mismatch compiler error.
//let failedList = [ 5; "six" ]

// In F#, functions can be stored in a list, as long as the functions
// have the same signature.

// Function doubleIt has the same signature as squareIt, declared previously.
//let squareIt = fun n -> n * n
let doubleIt = fun n -> 2 * n

// Functions squareIt and doubleIt can be stored together in a list.
let funList = [ squareIt; doubleIt ]

// Function squareIt cannot be stored in a list together with a function
// that has a different signature, such as the following body mass
// index (BMI) calculator.
let BMICalculator = fun ht wt ->
                    (float wt / float (squareIt ht)) * 703.0

// The following expression causes a type-mismatch compiler error.
//let failedFunList = [ squareIt; BMICalculator ]


// Tuples.

// Integers and strings.
let integerTuple = ( 1, -7 )
let stringTuple = ( "one", "two", "three" )

// A tuple does not require its elements to be of the same type.
let mixedTuple = ( 1, "two", 3.3 )

// Similarly, function elements in tuples can have different signatures.
let funTuple = ( squareIt, BMICalculator )

// Functions can be mixed with integers, strings, and other types in
// a tuple. Identifier num was declared previously.
//let num = 10
let moreMixedTuple = ( num, "two", 3.3, squareIt )

Para verificar se um nome de função armazenado em uma tupla é avaliado como uma função, o exemplo a seguir usa os operadores fst e snd para extrair o primeiro e o segundo elementos da tupla funAndArgTuple. O primeiro elemento na tupla é squareIt e o segundo elemento é num. O identificador num está associado em um exemplo anterior ao inteiro 10, um argumento válido para a função squareIt. A segunda expressão aplica o primeiro elemento na tupla ao segundo elemento na tupla: squareIt num.

// You can pull a function out of a tuple and apply it. Both squareIt and num
// were defined previously.
let funAndArgTuple = (squareIt, num)

// The following expression applies squareIt to num, returns 100, and
// then displays 100.
System.Console.WriteLine((fst funAndArgTuple)(snd funAndArgTuple))

Da mesma forma, assim como o identificador num e o inteiro 10 podem ser usados de forma intercambiável, o identificador squareIt e a expressão lambda podem ser usados fun n -> n * n.

// Make a tuple of values instead of identifiers.
let funAndArgTuple2 = ((fun n -> n * n), 10)

// The following expression applies a squaring function to 10, returns
// 100, and then displays 100.
System.Console.WriteLine((fst funAndArgTuple2)(snd funAndArgTuple2))

Passar o valor como um argumento

Se um valor tiver status de primeira classe em um idioma, você poderá passá-lo como um argumento para uma função. Por exemplo, é comum passar inteiros e cadeias de caracteres como argumentos. O código a seguir mostra inteiros e cadeias de caracteres passados como argumentos em F#.

// An integer is passed to squareIt. Both squareIt and num are defined in
// previous examples.
//let num = 10
//let squareIt = fun n -> n * n
System.Console.WriteLine(squareIt num)

// String.
// Function repeatString concatenates a string with itself.
let repeatString = fun s -> s + s

// A string is passed to repeatString. HelloHello is returned and displayed.
let greeting = "Hello"
System.Console.WriteLine(repeatString greeting)

Se as funções tiverem status de primeira classe, você deverá ser capaz de passá-las como argumentos da mesma maneira. Lembre-se de que essa é a primeira característica das funções de ordem superior.

No exemplo a seguir, a função applyIt tem dois parâmetros op e arg. Se você enviar uma função para a qual tem um parâmetro op e um argumento apropriado para a função arg, a função retornará o resultado da aplicação de op em arg. No exemplo a seguir, o argumento de função e o argumento inteiro são enviados da mesma maneira, usando seus nomes.

// Define the function, again using lambda expression syntax.
let applyIt = fun op arg -> op arg

// Send squareIt for the function, op, and num for the argument you want to
// apply squareIt to, arg. Both squareIt and num are defined in previous
// examples. The result returned and displayed is 100.
System.Console.WriteLine(applyIt squareIt num)

// The following expression shows the concise syntax for the previous function
// definition.
let applyIt2 op arg = op arg
// The following line also displays 100.
System.Console.WriteLine(applyIt2 squareIt num)

A capacidade de enviar uma função como argumento para outra função é subjacente a abstrações comuns em linguagens de programação funcionais, como operações de mapa ou filtro. Uma operação de mapa, por exemplo, é uma função de ordem superior que captura a computação compartilhada por funções que passam por uma lista, fazem algo a cada elemento e retornam uma lista dos resultados. Talvez você queira incrementar cada elemento em uma lista de inteiros, colocar cada elemento em quadrado ou alterar cada elemento em uma lista de cadeias de caracteres para maiúsculas. A parte propensa a erros da computação é o processo recursivo que percorre a lista e compila uma lista dos resultados a serem retornados. Essa parte é capturada na função de mapeamento. Tudo o que você precisa gravar para um aplicativo específico é a função que você deseja aplicar a cada elemento de lista individualmente (adicionar, elevar ao quadrado, alterar maiúsculas e minúsculas). Essa função é enviada como um argumento para a função de mapeamento, assim como squareIt é enviado para applyIt no exemplo anterior.

O F# fornece métodos de mapa para a maioria dos tipos de coleção, incluindo listas, matrizes e sequências. Os exemplos a seguir usam listas. A sintaxe é List.map <the function> <the list>.

// List integerList was defined previously:
//let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]

// You can send the function argument by name, if an appropriate function
// is available. The following expression uses squareIt.
let squareAll = List.map squareIt integerList

// The following line displays [1; 4; 9; 16; 25; 36; 49]
printfn "%A" squareAll

// Or you can define the action to apply to each list element inline.
// For example, no function that tests for even integers has been defined,
// so the following expression defines the appropriate function inline.
// The function returns true if n is even; otherwise it returns false.
let evenOrNot = List.map (fun n -> n % 2 = 0) integerList

// The following line displays [false; true; false; true; false; true; false]
printfn "%A" evenOrNot

Para obter mais informações, confira Listas.

Retornar o valor de uma chamada de função

Por fim, se uma função tiver o status de primeira classe em uma linguagem, você deverá ser capaz de devolvê-la como o valor de uma chamada de função, assim como você retorna outros tipos, como inteiros e cadeias de caracteres.

As chamadas de função a seguir retornam inteiros e os exibem.

// Function doubleIt is defined in a previous example.
//let doubleIt = fun n -> 2 * n
System.Console.WriteLine(doubleIt 3)
System.Console.WriteLine(squareIt 4)

A seguinte chamada de função retorna uma cadeia de caracteres.

// str is defined in a previous section.
//let str = "F#"
let lowercase = str.ToLower()

A chamada de função a seguir, declarada embutida, retorna um valor booliano. O valor exibido é True.

System.Console.WriteLine((fun n -> n % 2 = 1) 15)

A capacidade de retornar uma função como o valor de uma chamada de função é a segunda característica das funções de ordem superior. No exemplo a seguir, checkFor é definido como uma função que usa um argumento item e retorna uma nova função como seu valor. A função retornada usa uma lista como seu argumento lst e pesquisa item em lst. Se item estiver presente, a função retornará true. Se item não estiver presente, a função retornará false. Como na seção anterior, o código a seguir usa uma função de lista fornecida, List.exists, para pesquisar a lista.

let checkFor item =
    let functionToReturn = fun lst ->
                           List.exists (fun a -> a = item) lst
    functionToReturn

O código a seguir usa checkFor para criar uma nova função que usa um argumento, uma lista e pesquisa 7 na lista.

// integerList and stringList were defined earlier.
//let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]
//let stringList = [ "one"; "two"; "three" ]

// The returned function is given the name checkFor7.
let checkFor7 = checkFor 7

// The result displayed when checkFor7 is applied to integerList is True.
System.Console.WriteLine(checkFor7 integerList)

// The following code repeats the process for "seven" in stringList.
let checkForSeven = checkFor "seven"

// The result displayed is False.
System.Console.WriteLine(checkForSeven stringList)

O exemplo a seguir usa o status de primeira classe das funções em F# para declarar uma função compose, que retorna uma composição de dois argumentos de função.

// Function compose takes two arguments. Each argument is a function
// that takes one argument of the same type. The following declaration
// uses lambda expression syntax.
let compose =
    fun op1 op2 ->
        fun n ->
            op1 (op2 n)

// To clarify what you are returning, use a nested let expression:
let compose2 =
    fun op1 op2 ->
        // Use a let expression to build the function that will be returned.
        let funToReturn = fun n ->
                            op1 (op2 n)
        // Then just return it.
        funToReturn

// Or, integrating the more concise syntax:
let compose3 op1 op2 =
    let funToReturn = fun n ->
                        op1 (op2 n)
    funToReturn

Observação

Para obter uma versão ainda mais curta, consulte a seção a seguir, "Funções via currying".

O código a seguir envia duas funções como argumentos para compose, ambas com um único argumento do mesmo tipo. O valor retornado é uma nova função que é uma composição dos dois argumentos de função.

// Functions squareIt and doubleIt were defined in a previous example.
let doubleAndSquare = compose squareIt doubleIt
// The following expression doubles 3, squares 6, and returns and
// displays 36.
System.Console.WriteLine(doubleAndSquare 3)

let squareAndDouble = compose doubleIt squareIt
// The following expression squares 3, doubles 9, returns 18, and
// then displays 18.
System.Console.WriteLine(squareAndDouble 3)

Observação

F# fornece dois operadores << e >>, que compõem as funções. Por exemplo, let squareAndDouble2 = doubleIt << squareIt é equivalente a let squareAndDouble = compose doubleIt squareIt no exemplo anterior.

O exemplo a seguir de retorno de uma função como o valor de uma chamada de função cria um jogo de adivinhação simples. Para criar um jogo, chame makeGame com o valor que você deseja que alguém adivinhe enviado para target. O valor retornado da função makeGame é uma função que usa um argumento (o palpite) e relata se o palpite está correto.

let makeGame target =
    // Build a lambda expression that is the function that plays the game.
    let game = fun guess ->
                   if guess = target then
                      System.Console.WriteLine("You win!")
                   else
                      System.Console.WriteLine("Wrong. Try again.")
    // Now just return it.
    game

O código a seguir chama makeGame, enviando o valor 7 para target. O identificador playGame está associado à expressão lambda retornada. Portanto, playGame é uma função que usa como argumento um valor para guess.

let playGame = makeGame 7
// Send in some guesses.
playGame 2
playGame 9
playGame 7

// Output:
// Wrong. Try again.
// Wrong. Try again.
// You win!

// The following game specifies a character instead of an integer for target.
let alphaGame = makeGame 'q'
alphaGame 'c'
alphaGame 'r'
alphaGame 'j'
alphaGame 'q'

// Output:
// Wrong. Try again.
// Wrong. Try again.
// Wrong. Try again.
// You win!

Funções via currying

Muitos dos exemplos na seção anterior podem ser gravados de forma mais concisa aproveitando o currying implícito nas declarações da função F#. Currying é um processo que transforma uma função que tem mais de um parâmetro em uma série de funções inseridas, cada uma com um único parâmetro. Em F#, as funções que têm mais de um parâmetro são inerentemente transformadas via currying. Por exemplo, compose da seção anterior pode ser gravada conforme mostrado no estilo conciso a seguir, com três parâmetros.

let compose4 op1 op2 n = op1 (op2 n)

No entanto, o resultado é uma função de um parâmetro que retorna uma função de um parâmetro que, por sua vez, retorna outra função de um parâmetro, conforme exibido em compose4curried.

let compose4curried =
    fun op1 ->
        fun op2 ->
            fun n -> op1 (op2 n)

Essa função pode ser acessada de várias maneiras. Cada um dos exemplos a seguir retorna e exibe 18. Você pode substituir compose4 por compose4curried em qualquer um dos exemplos.

// Access one layer at a time.
System.Console.WriteLine(((compose4 doubleIt) squareIt) 3)

// Access as in the original compose examples, sending arguments for
// op1 and op2, then applying the resulting function to a value.
System.Console.WriteLine((compose4 doubleIt squareIt) 3)

// Access by sending all three arguments at the same time.
System.Console.WriteLine(compose4 doubleIt squareIt 3)

Para verificar se a função ainda funciona como antes, tente os casos de teste originais novamente.

let doubleAndSquare4 = compose4 squareIt doubleIt
// The following expression returns and displays 36.
System.Console.WriteLine(doubleAndSquare4 3)

let squareAndDouble4 = compose4 doubleIt squareIt
// The following expression returns and displays 18.
System.Console.WriteLine(squareAndDouble4 3)

Observação

Você pode restringir o currying colocando parâmetros em tuplas. Para obter mais informações, consulte "Padrões de parâmetro" em Parâmetros e argumentos.

O exemplo a seguir usa currying implícito para gravar uma versão mais curta de makeGame. Os detalhes de como makeGame constrói e retorna a função game são menos explícitos nesse formato, mas você pode verificar usando os casos de teste originais, pois o resultado será o mesmo.

let makeGame2 target guess =
    if guess = target then
       System.Console.WriteLine("You win!")
    else
       System.Console.WriteLine("Wrong. Try again.")

let playGame2 = makeGame2 7
playGame2 2
playGame2 9
playGame2 7

let alphaGame2 = makeGame2 'q'
alphaGame2 'c'
alphaGame2 'r'
alphaGame2 'j'
alphaGame2 'q'

Para obter mais informações sobre currying, consulte "Aplicação parcial dos argumentos", em Funções.

O identificador e a definição de função são intercambiáveis

O nome da variável num nos exemplos anteriores é avaliado como o inteiro 10 e não é surpresa que, onde num é válido, 10 também seja válido. O mesmo se aplica aos identificadores de função e seus valores: em qualquer lugar que o nome da função possa ser usado, a expressão lambda à qual ela está associada pode ser usada.

O exemplo a seguir define uma função Boolean chamada isNegativee usa o nome e a definição da função de forma intercambiável. Os próximos três exemplos retornam e exibem False.

let isNegative = fun n -> n < 0

// This example uses the names of the function argument and the integer
// argument. Identifier num is defined in a previous example.
//let num = 10
System.Console.WriteLine(applyIt isNegative num)

// This example substitutes the value that num is bound to for num, and the
// value that isNegative is bound to for isNegative.
System.Console.WriteLine(applyIt (fun n -> n < 0) 10)

Para dar um passo adiante, substitua o valor a que applyIt está associado por applyIt.

System.Console.WriteLine((fun op arg -> op arg) (fun n -> n < 0)  10)

Funções são valores de primeira classe no F#

Os exemplos nas seções anteriores demonstram que as funções no F# atendem aos critérios para serem valores de primeira classe no F#:

  • Você pode associar um identificador a uma definição de função.
let squareIt = fun n -> n * n
  • Você pode armazenar uma função em uma estrutura de dados.
let funTuple2 = ( BMICalculator, fun n -> n * n )
  • Você pode passar uma função como um argumento.
let increments = List.map (fun n -> n + 1) [ 1; 2; 3; 4; 5; 6; 7 ]
  • Você pode retornar uma função como o valor de uma chamada de função.
let checkFor item =
    let functionToReturn = fun lst ->
                           List.exists (fun a -> a = item) lst
    functionToReturn

Para obter mais informações sobre F#, consulte Referência da linguagem F#.

Exemplo

Descrição

O código a seguir contém todos os exemplos neste tópico.

Código

// ** GIVE THE VALUE A NAME **


// Integer and string.
let num = 10
let str = "F#"



let squareIt = fun n -> n * n



let squareIt2 n = n * n



// ** STORE THE VALUE IN A DATA STRUCTURE **


// Lists.

// Storing integers and strings.
let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]
let stringList = [ "one"; "two"; "three" ]

// You cannot mix types in a list. The following declaration causes a
// type-mismatch compiler error.
//let failedList = [ 5; "six" ]

// In F#, functions can be stored in a list, as long as the functions
// have the same signature.

// Function doubleIt has the same signature as squareIt, declared previously.
//let squareIt = fun n -> n * n
let doubleIt = fun n -> 2 * n

// Functions squareIt and doubleIt can be stored together in a list.
let funList = [ squareIt; doubleIt ]

// Function squareIt cannot be stored in a list together with a function
// that has a different signature, such as the following body mass
// index (BMI) calculator.
let BMICalculator = fun ht wt ->
                    (float wt / float (squareIt ht)) * 703.0

// The following expression causes a type-mismatch compiler error.
//let failedFunList = [ squareIt; BMICalculator ]


// Tuples.

// Integers and strings.
let integerTuple = ( 1, -7 )
let stringTuple = ( "one", "two", "three" )

// A tuple does not require its elements to be of the same type.
let mixedTuple = ( 1, "two", 3.3 )

// Similarly, function elements in tuples can have different signatures.
let funTuple = ( squareIt, BMICalculator )

// Functions can be mixed with integers, strings, and other types in
// a tuple. Identifier num was declared previously.
//let num = 10
let moreMixedTuple = ( num, "two", 3.3, squareIt )



// You can pull a function out of a tuple and apply it. Both squareIt and num
// were defined previously.
let funAndArgTuple = (squareIt, num)

// The following expression applies squareIt to num, returns 100, and
// then displays 100.
System.Console.WriteLine((fst funAndArgTuple)(snd funAndArgTuple))



// Make a list of values instead of identifiers.
let funAndArgTuple2 = ((fun n -> n * n), 10)

// The following expression applies a squaring function to 10, returns
// 100, and then displays 100.
System.Console.WriteLine((fst funAndArgTuple2)(snd funAndArgTuple2))



// ** PASS THE VALUE AS AN ARGUMENT **


// An integer is passed to squareIt. Both squareIt and num are defined in
// previous examples.
//let num = 10
//let squareIt = fun n -> n * n
System.Console.WriteLine(squareIt num)

// String.
// Function repeatString concatenates a string with itself.
let repeatString = fun s -> s + s

// A string is passed to repeatString. HelloHello is returned and displayed.
let greeting = "Hello"
System.Console.WriteLine(repeatString greeting)



// Define the function, again using lambda expression syntax.
let applyIt = fun op arg -> op arg

// Send squareIt for the function, op, and num for the argument you want to
// apply squareIt to, arg. Both squareIt and num are defined in previous
// examples. The result returned and displayed is 100.
System.Console.WriteLine(applyIt squareIt num)

// The following expression shows the concise syntax for the previous function
// definition.
let applyIt2 op arg = op arg
// The following line also displays 100.
System.Console.WriteLine(applyIt2 squareIt num)



// List integerList was defined previously:
//let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]

// You can send the function argument by name, if an appropriate function
// is available. The following expression uses squareIt.
let squareAll = List.map squareIt integerList

// The following line displays [1; 4; 9; 16; 25; 36; 49]
printfn "%A" squareAll

// Or you can define the action to apply to each list element inline.
// For example, no function that tests for even integers has been defined,
// so the following expression defines the appropriate function inline.
// The function returns true if n is even; otherwise it returns false.
let evenOrNot = List.map (fun n -> n % 2 = 0) integerList

// The following line displays [false; true; false; true; false; true; false]
printfn "%A" evenOrNot



// ** RETURN THE VALUE FROM A FUNCTION CALL **


// Function doubleIt is defined in a previous example.
//let doubleIt = fun n -> 2 * n
System.Console.WriteLine(doubleIt 3)
System.Console.WriteLine(squareIt 4)


// The following function call returns a string:

// str is defined in a previous section.
//let str = "F#"
let lowercase = str.ToLower()



System.Console.WriteLine((fun n -> n % 2 = 1) 15)



let checkFor item =
    let functionToReturn = fun lst ->
                           List.exists (fun a -> a = item) lst
    functionToReturn



// integerList and stringList were defined earlier.
//let integerList = [ 1; 2; 3; 4; 5; 6; 7 ]
//let stringList = [ "one"; "two"; "three" ]

// The returned function is given the name checkFor7.
let checkFor7 = checkFor 7

// The result displayed when checkFor7 is applied to integerList is True.
System.Console.WriteLine(checkFor7 integerList)

// The following code repeats the process for "seven" in stringList.
let checkForSeven = checkFor "seven"

// The result displayed is False.
System.Console.WriteLine(checkForSeven stringList)



// Function compose takes two arguments. Each argument is a function
// that takes one argument of the same type. The following declaration
// uses lambda expression syntax.
let compose =
    fun op1 op2 ->
        fun n ->
            op1 (op2 n)

// To clarify what you are returning, use a nested let expression:
let compose2 =
    fun op1 op2 ->
        // Use a let expression to build the function that will be returned.
        let funToReturn = fun n ->
                            op1 (op2 n)
        // Then just return it.
        funToReturn

// Or, integrating the more concise syntax:
let compose3 op1 op2 =
    let funToReturn = fun n ->
                        op1 (op2 n)
    funToReturn



// Functions squareIt and doubleIt were defined in a previous example.
let doubleAndSquare = compose squareIt doubleIt
// The following expression doubles 3, squares 6, and returns and
// displays 36.
System.Console.WriteLine(doubleAndSquare 3)

let squareAndDouble = compose doubleIt squareIt
// The following expression squares 3, doubles 9, returns 18, and
// then displays 18.
System.Console.WriteLine(squareAndDouble 3)



let makeGame target =
    // Build a lambda expression that is the function that plays the game.
    let game = fun guess ->
                   if guess = target then
                      System.Console.WriteLine("You win!")
                   else
                      System.Console.WriteLine("Wrong. Try again.")
    // Now just return it.
    game



let playGame = makeGame 7
// Send in some guesses.
playGame 2
playGame 9
playGame 7

// Output:
// Wrong. Try again.
// Wrong. Try again.
// You win!

// The following game specifies a character instead of an integer for target.
let alphaGame = makeGame 'q'
alphaGame 'c'
alphaGame 'r'
alphaGame 'j'
alphaGame 'q'

// Output:
// Wrong. Try again.
// Wrong. Try again.
// Wrong. Try again.
// You win!



// ** CURRIED FUNCTIONS **


let compose4 op1 op2 n = op1 (op2 n)



let compose4curried =
    fun op1 ->
        fun op2 ->
            fun n -> op1 (op2 n)



// Access one layer at a time.
System.Console.WriteLine(((compose4 doubleIt) squareIt) 3)

// Access as in the original compose examples, sending arguments for
// op1 and op2, then applying the resulting function to a value.
System.Console.WriteLine((compose4 doubleIt squareIt) 3)

// Access by sending all three arguments at the same time.
System.Console.WriteLine(compose4 doubleIt squareIt 3)



let doubleAndSquare4 = compose4 squareIt doubleIt
// The following expression returns and displays 36.
System.Console.WriteLine(doubleAndSquare4 3)

let squareAndDouble4 = compose4 doubleIt squareIt
// The following expression returns and displays 18.
System.Console.WriteLine(squareAndDouble4 3)



let makeGame2 target guess =
    if guess = target then
       System.Console.WriteLine("You win!")
    else
       System.Console.WriteLine("Wrong. Try again.")

let playGame2 = makeGame2 7
playGame2 2
playGame2 9
playGame2 7

let alphaGame2 = makeGame2 'q'
alphaGame2 'c'
alphaGame2 'r'
alphaGame2 'j'
alphaGame2 'q'



// ** IDENTIFIER AND FUNCTION DEFINITION ARE INTERCHANGEABLE **


let isNegative = fun n -> n < 0

// This example uses the names of the function argument and the integer
// argument. Identifier num is defined in a previous example.
//let num = 10
System.Console.WriteLine(applyIt isNegative num)

// This example substitutes the value that num is bound to for num, and the
// value that isNegative is bound to for isNegative.
System.Console.WriteLine(applyIt (fun n -> n < 0) 10)



System.Console.WriteLine((fun op arg -> op arg) (fun n -> n < 0)  10)



// ** FUNCTIONS ARE FIRST-CLASS VALUES IN F# **

//let squareIt = fun n -> n * n


let funTuple2 = ( BMICalculator, fun n -> n * n )



let increments = List.map (fun n -> n + 1) [ 1; 2; 3; 4; 5; 6; 7 ]


//let checkFor item =
//    let functionToReturn = fun lst ->
//                           List.exists (fun a -> a = item) lst
//    functionToReturn

Confira também