Formation F#
  • Intro
  • Bases
    • Le F♯, c'est quoi ?
    • Syntaxe
    • Premiers concepts
    • 🍔 Quiz
  • Fonctions
    • Signature
    • Fonctions
    • Fonctions standard
    • OpĂ©rateurs
    • Fonctions : complĂ©ments
    • 🍔 Quiz
    • 📜 RĂ©cap’
  • Types composites
    • GĂ©nĂ©ralitĂ©s
    • Tuples
    • Records
    • Unions
    • Enums
    • Records anonymes
    • Types valeur
    • 🍔 Quiz
  • Types : ComplĂ©ments
    • Type unit
    • GĂ©nĂ©riques
    • Types flexibles
    • UnitĂ©s de mesure
    • Conversion
    • Exceptions F#
  • Pattern matching
    • Patterns
    • Match expression
    • 🚀 Active Patterns
    • 📜 RĂ©cap’
  • Collections
    • Vue d'ensemble
    • Types
    • Fonctions gĂ©nĂ©riques
    • Fonctions spĂ©cifiques
    • 🍔 Quiz
    • 📜 RĂ©cap’
  • Programmation asynchrone
    • Workflow asynchrone
    • Interop avec la TPL .NET
    • 📜 RĂ©cap’
  • Types monadiques
    • Type Option
    • Type Result
    • Smart constructor
    • 🚀 Computation expression (CE)
    • 🚀 CE - Fondements thĂ©oriques
    • 📜 RĂ©cap’
  • Module & namespace
    • Vue d'ensemble
    • Namespace
    • Module
    • 🍔 Quiz
    • 📜 RĂ©cap’
  • OrientĂ©-objet
    • Introduction
    • Membres
    • Extensions de type
    • Classe, structure
    • Interface
    • Expression objet
    • Recommandations
  • 🩚 Aller plus loin
Propulsé par GitBook
Sur cette page
  • Question 1
  • RĂ©ponse
  • Question 2
  • RĂ©ponse
  • Question 3
  • RĂ©ponse
  • Question 4
  • RĂ©ponse
  • Question 5
  • RĂ©ponse
  • Question 6
  • RĂ©ponse
  • Question 7
  • RĂ©ponse

Cet article vous a-t-il été utile ?

Modifier sur GitHub
  1. Fonctions

🍔 Quiz

Question 1

Comment dĂ©finir la valeur de retour v d'une fonction f ? ⏱ 10’’

A. Il suffit de nommer la valeur result

B. Faire return v

C. v constitue la derniĂšre ligne de f

Réponse

A. Il suffit de nommer la valeur result ❌

B. Faire return v ❌

C. v constitue la derniùre ligne de f ✅

Question 2

Comment Ă©crire fonction add prenant 2 strings et renvoyant un int ⏱ 20’’

A. let add a b = a + b

B. let add (a: string) (b: string) = (int a) + (int b)

C. let add (a: string) (b: string) : int = a + b

Réponse

A. let add a b = a + b ❌

Mauvais type inféré pour a et b : int

B. let add (a: string) (b: string) = (int a) + (int b) ✅

  • Il faut spĂ©cifier le type de a et b.

  • Il faut les convertir en int.

  • Le type de retour int peut ĂȘtre infĂ©rĂ©.

C. let add (a: string) (b: string) : int = a + b

Ici, + concatĂšne les strings.

Question 3

Que fait le code add >> multiply ? ⏱ 10’’

A. Créer un pipeline

B. Définir une fonction

C. Créer une composition

Réponse

A. CrĂ©er un pipeline ❌

B. DĂ©finir une fonction ❌ (mais ce n'est pas faux)

C. CrĂ©er une composition ✅

Question 4

A. let ? _ = ()

B. let ? x = x

C. let ? f x = f x

D. let ? x f = f x

E. let ? f g x = g (f x)

💡 Il peut s'agir d'opĂ©rateurs

Réponse

A. let inline ignore _ = ()

B. let id x = x

C. let inline (<|) func arg = func arg

D. let inline (|>) arg func = func arg

E. let inline (>>) func1 func2 x = func2 (func1 x)

Question 5

Que signifie ces signatures de fonction : Combien de paramĂštres ? Leur type ? Le type du retour ? ⏱ 60’’

A. int -> unit

B. unit -> int

C. string -> string -> string

D. ('T -> bool) -> 'T list -> 'T list

Réponse

A. int -> unit

1 paramÚtre int - pas de valeur renvoyée

B. unit -> int

Aucun paramĂštre - renvoie un int

C. string -> string -> string

2 paramĂštres string - renvoie une string

D. ('T -> bool) -> 'T list -> 'T list

2 paramÚtres : un prédicat et une liste - renvoie une liste

💡 Il s'agit de la fonction List.filter predicate list

Question 6

Quelle est la signature de la fonction h ci-dessous ? ⏱ 30’’

let f x = x + 1
let g x y = $"%i{x} + %i{y}"
let h = f >> g

A. int -> int

B. int -> string

C. int -> int -> string

D. int -> int -> int

💡 %i{a} indique que a est un int

Réponse

C. int -> int -> string ✅

let f x = x + 1 → f: (x: int) -> int » 1 → int → x: int → x + 1: int

let g x y = $"{+x} + {+y}" → (x: int) -> (y: int) -> string » %i{x} → int » $"..." → string

let h = f >> g » h peut s'Ă©crire let h x y = g (f x) y » MĂȘme x que f → int, mĂȘme y que g → int

☝ Note : cet exemple illustre une mauvaise utilisation de >> car les fonctions composĂ©es ont une aritĂ© diffĂ©rente (f a 1 paramĂštre, g en a 2)

Question 7

Combien vaut f 2 ? ⏱ 10’’

let f = (-) 1;
f 2 // ?

A. 1

B. 3

C. -1

Réponse

let f = (-) 1
f 2 // ?

C. -1 ❗

  • Contre-intuitif, non ? On s'attend Ă  ce que f dĂ©crĂ©mente de 1.

  • On comprend mieux ce qui se passe en Ă©crivant f ainsi : let f x = 1 - x

💡 Astuce : la fonction qui dĂ©crĂ©mente de 1 peut s'Ă©crire :

  • let f = (+) -1 (cela marche ici car + est commutatif alors que - ne l'est pas)

  • let f x = x - 1

PrĂ©cĂ©dentFonctions : complĂ©mentsSuivant📜 RĂ©cap’

DerniĂšre mise Ă  jour il y a 2 ans

Cet article vous a-t-il été utile ?

Retrouvez le nom de ces fonctions ⏱ 60’’

Ignore :

Identity :

Pipe Left :

Pipe Right :

Compose Right :

Core
prim-types.fs#L459
prim-types.fs#L4831
prim-types.fs#L3914
prim-types.fs#L3908
prim-types.fs#L3920