Partilhar via


Usando funções em 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 type 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 puder fazer com valores dos outros tipos integrados, com um grau comparável de esforço.

  • Você pode dar nomes de valores de 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.

Dê um nome ao valor

Se uma função for um valor de primeira classe, você deve ser capaz de nomeá-la, assim como pode nomear inteiros, cadeias de caracteres e outros tipos internos. Isto é referido na literatura de programação funcional como a ligação de um identificador a um valor. F# usa let associações para vincular 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 vinculando o identificador squareIt à expressãofun n -> n * n lambda. Function squareIt tem um parâmetro, ne retorna o quadrado desse parâmetro.

let squareIt = fun n -> n * n

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

let squareIt2 n = n * n

Os exemplos que se seguem 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 escritas com a sintaxe concisa. Alguns dos exemplos são escritos em ambos os sentidos.

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 de fato avalia uma função, o exemplo a seguir usa os fst operadores e snd para extrair o primeiro e o segundo elementos da tupla funAndArgTuple. O primeiro elemento na tupla é squareIt e o segundo elemento é num. Identifier num é vinculado em um exemplo anterior ao inteiro 10, um argumento válido para a squareIt função. A segunda expressão aplica o primeiro elemento da tupla ao segundo elemento da 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 identificador num e inteiro 10 podem ser usados de forma intercambiável, identificador e expressão fun n -> n * nlambda também podemsquareIt.

// 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ê deve ser capaz de passá-las como argumentos da mesma maneira. Lembre-se que esta é a primeira característica das funções de ordem superior.

No exemplo a seguir, a função applyIt tem dois parâmetros e arg. op Se você enviar uma função que tenha um parâmetro para op e um argumento apropriado para a função para arg, a função retornará o resultado da aplicação op a arg. No exemplo a seguir, o argumento function e o argumento integer 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 está 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 percorrem uma lista, fazem algo para cada elemento e, em seguida, retornam uma lista dos resultados. Talvez você queira incrementar cada elemento em uma lista de inteiros, ou quadrar cada elemento, ou alterar cada elemento em uma lista de cadeias de caracteres para maiúsculas. A parte propensa a erros do cálculo é o processo recursivo que percorre a lista e cria uma lista dos resultados a serem retornados. Essa parte é capturada na função de mapeamento. Tudo o que você precisa escrever para um aplicativo específico é a função que você deseja aplicar a cada elemento da lista individualmente (adicionar, quadratura, alterar maiúsculas e minúsculas). Essa função é enviada como um argumento para a função de mapeamento, assim como squareIt é enviada no applyIt exemplo anterior.

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, consulte Listas.

Retornar o valor de uma chamada de função

Finalmente, se uma função tiver status de primeira classe em um idioma, você deve ser capaz de retorná-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 chamada de função a seguir 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 em linha, retorna um valor booleano. 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, checkForitemé definida como uma função que usa um argumento e retorna uma nova função como seu valor. A função retornada usa uma lista como argumento lste procura 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 procura 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 de 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

Nota

Para uma versão ainda mais curta, consulte a seção a seguir, "Curried Functions".

O código a seguir envia duas funções como argumentos para compose, ambas usam um único argumento do mesmo tipo. O valor de retorno é 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)

Nota

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

O exemplo a seguir de retornar uma função como o valor de uma chamada de função cria um jogo de adivinhação simples. Para criar um jogo, ligue makeGame com o valor que você deseja que alguém adivinhe enviado para target. O valor de retorno 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á vinculado à expressão lambda retornada. Portanto, playGame é uma função que toma como seu único 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 Curried

Muitos dos exemplos na seção anterior podem ser escritos de forma mais concisa, aproveitando o currying implícito em 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 incorporadas, cada uma das quais tem um único parâmetro. Em F#, as funções que têm mais de um parâmetro são inerentemente curries. Por exemplo, compose a partir 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 que retorna uma função de um parâmetro que, por sua vez, retorna outra função de um parâmetro, como mostrado em compose4curried.

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

Pode aceder a esta função de várias formas. 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)

Nota

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 escrever uma versão mais curta do makeGame. Os detalhes de como makeGame constrói e retorna a game função são menos explícitos nesse 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 "Aplicação parcial de argumentos" em Funções.

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

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

O exemplo a seguir define uma Boolean função chamada isNegative, e usa o nome da função e a definição da função de forma intercambiável. Os próximos três exemplos todos 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 ir um passo adiante, substitua o valor vinculado applyIt por applyIt.

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

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

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

  • Você pode vincular 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 a Referência de linguagem F#.

Exemplo

Description

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

Consulte também