Share via


Funções como valores de classe do primeiro-(F#)

Uma característica de definição de linguagens de programação funcionais é a elevação de funções para o status de primeira classe. Você poderá fazer com uma função que você pode fazer com os valores dos tipos internos e ser capaz de fazê-lo com um grau comparável de esforço.

Medidas típicas do status de primeira classe incluem o seguinte:

  • Você pode vincular um identificador para o valor? Ou seja, pode dá-lo um nome?

  • Você pode armazenar o valor em uma estrutura de dados, como, por exemplo, uma lista?

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

  • Você pode retornar o valor como o valor de uma chamada de função?

As duas últimas medidas que definem o que é conhecido como operações de ordem superior ou funções de ordem superior. Funções de ordem superior aceitam funções como argumentos e retornam a funções como o valor de chamadas de função. Essas operações oferecem suporte a tais mainstays da programação funcional como mapeamento de funções e a composição de funções.

Nomeie o valor

Se uma função é um valor de primeira classe, você deve ser capaz de nomeá-lo, exatamente como você pode nomear inteiros, seqüências e outros tipos internos. Isso é conhecido na literatura de programação funcional como um identificador de ligação para um valor. A usa F# permitem expressões para ligar os nomes de 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 tão facilmente. O exemplo a seguir define uma função chamada squareIt , o identificador de ligação squareIt para o expressão lambda fun n -> n * n. Função squareIt tem um parâmetro, n, e ela retorna o quadrado do parâmetro.

let squareIt = fun n -> n * n

F# fornece a seguinte sintaxe mais concisa para alcançar o mesmo resultado com menos digitação.

let squareIt2 n = n * n

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

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 nas listas e no 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 um nome de função armazenado em uma tupla na verdade avaliar uma função, o exemplo a seguir usa a fst e snd operadores para extrair os elementos de primeiro e segundo da tupla funAndArgTuple. O primeiro elemento na tupla é squareIt e o segundo elemento é num. Identificador num ligado em um exemplo anterior para o inteiro 10, um argumento válido para o squareIt função. A segunda expressão aplica-se o primeiro elemento na tupla do 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, apenas como identificador num e o inteiro 10 pode ser usados alternadamente, pode ser identificador squareIt e a expressão lambda fun n -> n * n.

// 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))

Passar o valor como um argumento.

Se um valor tiver o status de primeira classe em um idioma, passá-lo como um argumento para uma função. Por exemplo, é comum passar inteiros e seqüências de caracteres como argumentos. O código a seguir mostra os números inteiros e seqüências de caracteres passadas 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 têm o status de primeira classe, você deve ser capaz para passá-las como argumentos da mesma maneira. Lembre-se de que esta é 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 em uma função que tem um parâmetro para op e um argumento apropriado para a função arg, a função retorna o resultado da aplicação op para arg. O exemplo a seguir, o argumento de função e o argumento de inteiro são enviados da mesma forma, 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 um argumento para outra função fica abstrações comuns em linguagens de programação funcionais, como, por exemplo, as 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 pelas funções que percorrer uma lista, faça algo para cada elemento e, em seguida, retornam uma lista dos resultados. Convém incrementar a cada elemento em uma lista de números inteiros, ou quadrado de cada elemento ou alterar cada elemento em uma lista de seqüências de caracteres em maiúsculas. A parte propensa a computação é o processo recursivo que percorre a lista e cria uma lista dos resultados para retornar. Essa parte é capturado na função de mapeamento. Tudo o que você precisa escrever um aplicativo específico é a função que você deseja aplicar a cada elemento da lista individualmente (adição, elevar, alterando o caso). Que função é enviada como um argumento para a função de mapeamento, assim como squareIt é enviada para applyIt o exemplo anterior.

F# fornece métodos de mapa para a maioria dos tipos de coleção, incluindo lista, arrays, e define. 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, consulte Listas (F#).

O valor de retorno de chamada de função

Finalmente, se uma função tiver o status de primeira classe em um idioma, você deve ser capaz para retorná-lo como o valor de uma chamada de função, da mesma forma que você retornar a outros tipos, como, por exemplo, inteiros e seqüências de caracteres.

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

// 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 chamada de função a seguir retorna uma seqüência de caracteres.

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

A seguinte chamada de função declarados embutidos, retorna um valor booleano. O valor exibido é True.

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

A capacidade de retornar a 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 tem um argumento, iteme retorna uma nova função como valor. A função retornada leva a uma lista como seu argumento lste procura por item na 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 da lista fornecida, List.exists, para pesquisar a lista.

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

O seguinte código usa checkFor para criar uma nova função que leva um argumento, uma lista e procura por 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 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 expresson 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çãoObservação

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

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

// 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çãoObservação

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

O exemplo a seguir para retornar a uma função como o valor de uma chamada de função cria um simples jogo de adivinhação. Para criar um jogo, chame makeGame com o valor que você deseja que alguém adivinhar enviada em target. O valor de retorno da função makeGame é uma função que leva um argumento (a estimativa) e informa se a estimativa está correta.

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 seguinte código chama makeGame, o valor de envio 7 para target. Identificador playGame está vinculado à expressão lambda retornada. Portanto, playGame é uma função que toma como seu uma 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 de curried

Muitos dos exemplos na seção anterior podem ser gravados forma mais concisa, aproveitando o implícito currying nas declarações de 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 internas, cada qual com um único parâmetro. No F#, a funções que têm mais de um parâmetro são inerentemente curried. Por exemplo, compose da seção anterior pode ser escrito como mostrado no seguinte estilo conciso, com três parâmetros.

let compose4 op1 op2 n = op1 (op2 n)

No entanto, o resultado é uma função de um parâmetro retorna uma função de um parâmetro que por sua vez, retorna outra função de um parâmetro, como mostrado na compose4curried.

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

Você pode acessar essa função de várias maneiras. Cada um dos exemplos a seguir retorna e exibe a 18. Você pode substituir compose4 com 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 fazia 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çãoObservação

Você pode restringir a currying colocando os parâmetros de tuplas. Para obter mais informações, consulte "Padrões de parâmetro" in Parâmetros e argumentos (F#).

O exemplo a seguir usa o currying implícita para gravar uma versão mais curta do makeGame. Os detalhes de como makeGame constrói e retorna o game função são menos explícitos neste formato, mas você pode verificar usando os casos de teste originais que o resultado é 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 "Parcial dos argumentos de aplicativo" in Funções (F#).

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

O nome da variável num no anterior avalia de exemplos para o inteiro 10, e não é surpresa que num é válido, 10 também é válido. O mesmo é verdadeiro de identificadores de função e seus valores: em qualquer lugar, que o nome da função pode ser usado, a expressão lambda ao qual está vinculado pode ser usada.

O exemplo a seguir define uma Boolean a função chamada isNegativee usa o nome da função e a definição da função alternadamente. Os próximos três exemplos todos retornar e exibir 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 ir um pouco mais, substituir o valor que applyIt está vinculado para applyIt.

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

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

Os exemplos nas seções anteriores demonstram que funções no F# satisfazem os critérios para sendo os valores de primeira classe no F#:

  • Você pode vincular um identificador para 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 a 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 O que há de Novo no Visual F# 2010. ou Referência de linguagem do 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 expresson 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

Consulte também

Referência

Tuplas (F#)

Funções (F#)

Deixe as ligações (F#)

Expressões lambda: A diversão de palavra-chave (F#)

Outros recursos

Listas (F#)