13 people like it.

F# Koans

An online version of the F# Koans for use with tryfsharp

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
134: 
135: 
136: 
137: 
138: 
139: 
140: 
141: 
142: 
143: 
144: 
145: 
146: 
147: 
148: 
149: 
150: 
151: 
152: 
153: 
154: 
155: 
156: 
157: 
158: 
159: 
160: 
161: 
162: 
163: 
164: 
165: 
166: 
167: 
168: 
169: 
170: 
171: 
172: 
173: 
174: 
175: 
176: 
177: 
178: 
179: 
180: 
181: 
182: 
183: 
184: 
185: 
186: 
187: 
188: 
189: 
190: 
191: 
192: 
193: 
194: 
195: 
196: 
197: 
198: 
199: 
200: 
201: 
202: 
203: 
204: 
205: 
206: 
207: 
208: 
209: 
210: 
211: 
212: 
213: 
214: 
215: 
216: 
217: 
218: 
219: 
220: 
221: 
222: 
223: 
224: 
225: 
226: 
227: 
228: 
229: 
230: 
231: 
232: 
233: 
234: 
235: 
236: 
237: 
238: 
239: 
240: 
241: 
242: 
243: 
244: 
245: 
246: 
247: 
248: 
249: 
250: 
251: 
252: 
253: 
254: 
255: 
256: 
257: 
258: 
259: 
260: 
261: 
262: 
263: 
264: 
265: 
266: 
267: 
268: 
269: 
270: 
271: 
272: 
273: 
274: 
275: 
276: 
277: 
278: 
279: 
280: 
281: 
282: 
283: 
284: 
285: 
286: 
287: 
288: 
289: 
290: 
291: 
292: 
293: 
294: 
295: 
296: 
297: 
298: 
299: 
300: 
301: 
302: 
303: 
304: 
305: 
306: 
307: 
308: 
309: 
310: 
311: 
312: 
313: 
314: 
315: 
316: 
317: 
318: 
319: 
320: 
321: 
322: 
323: 
324: 
325: 
326: 
327: 
328: 
329: 
330: 
331: 
332: 
333: 
334: 
335: 
336: 
337: 
338: 
339: 
340: 
341: 
342: 
343: 
344: 
345: 
346: 
347: 
348: 
349: 
350: 
351: 
352: 
353: 
354: 
355: 
356: 
357: 
358: 
359: 
360: 
361: 
362: 
363: 
364: 
365: 
366: 
367: 
368: 
369: 
370: 
371: 
372: 
373: 
374: 
375: 
376: 
377: 
378: 
379: 
380: 
381: 
382: 
383: 
384: 
385: 
386: 
387: 
388: 
389: 
390: 
391: 
392: 
393: 
394: 
395: 
396: 
397: 
398: 
399: 
400: 
401: 
402: 
403: 
404: 
405: 
406: 
407: 
408: 
409: 
410: 
411: 
412: 
413: 
414: 
415: 
416: 
417: 
418: 
419: 
420: 
421: 
422: 
423: 
424: 
425: 
426: 
427: 
428: 
429: 
430: 
431: 
432: 
433: 
434: 
435: 
436: 
437: 
438: 
439: 
440: 
441: 
442: 
443: 
444: 
445: 
446: 
447: 
448: 
449: 
450: 
451: 
452: 
453: 
454: 
455: 
456: 
457: 
458: 
459: 
460: 
461: 
462: 
463: 
464: 
465: 
466: 
467: 
468: 
469: 
470: 
471: 
472: 
473: 
474: 
475: 
476: 
477: 
478: 
479: 
480: 
481: 
//---------------------------------------------------------------
// Overview
//
// Below is a set of exercises designed to get you familiar 
// with F#. By the time you're done, you'll have a basic 
// understanding of the syntax of F# and learn a little more
// about functional programming in general.
//
// Answering Problems
// 
// This is where the fun begins! Each dashed section contains an 
// example designed to teach you a lesson about the F# language. 
// If you highlight the code in an example and execute it (use 
// Ctrl+Enter or the run button) it will initially fail. Your
// job is to fill in the blanks to make it pass. With each
// passing section, you'll learn more about F#, and add another
// weapon to your F# programming arsenal.
//
// Start by highlighitng the section below and running it. Once
// you see it fail, replace the __ with 2 to make it pass.
//---------------------------------------------------------------

// ---- about asserts -------------------------------------------

let expected_value = 1 + 1
let actual_value = __

AssertEquality expected_value actual_value

//Easy, right? Try the next one.

//---------------------------------------------------------------
 
// ---- more about asserts --------------------------------------

AssertEquality "foo" __

//---------------------------------------------------------------

//---------------------------------------------------------------
// About Let
//
// The let keyword is one of the most fundamental parts of F#.
// You'll use it in almost every line of F# code you write, so
// let's get to know it well! (no pun intended)
//---------------------------------------------------------------

// ---- let binds a name to a value -----------------------------

let x = 50
        
AssertEquality x __
    
//---------------------------------------------------------------

// ---- let infers the type of values when it can ---------------

(* In F#, values created with let are inferred to have a type like
   "int" for integer values, "string" for text values, and "bool" 
   for true or false values. *)

let x = 50
let typeOfX = x.GetType()
AssertEquality typeOfX typeof<int>

let y = "a string"
let expectedType = y.GetType()
AssertEquality expectedType typeof<FILL_ME_IN>

//---------------------------------------------------------------

// ---- you can make the types explicit -------------------------

let (x:int) = 42
let typeOfX = x.GetType()

let y:string = "forty two"
let typeOfY = y.GetType()

AssertEquality typeOfX typeof<FILL_ME_IN>
AssertEquality typeOfY typeof<FILL_ME_IN>

(* You don't usually need to provide explicit type annotations 
   types for local varaibles, but type annotations can come in 
   handy in other contexts as you'll see later. *)

//---------------------------------------------------------------
    
// ---- modifying the value of variables ------------------------

let mutable x = 100
x <- 200

AssertEquality x __

//---------------------------------------------------------------

// ---- you can't modify a value if it isn't mutable ------------

let x = 50

//What happens if you try to run the following line of code?
x <- 100

//NOTE: Although you can't modify immutable values, it is 
//      possible to reuse the name of a value in some cases 
//      using "shadowing".
let x = 100
 
AssertEquality x __

//---------------------------------------------------------------

//---------------------------------------------------------------
// About Functions
//
// Now that you've seen how to bind a name to a value with let,
// you'll learn to use the let keyword to create functions.
//---------------------------------------------------------------

// ---- creating functions with let -----------------------------

(* By default, F# is whitespace sensitive. For functions, this 
   means that the last line of a function is its return value,
   and the body of a function is denoted by indentation. *)

let add x y =
    x + y

let result1 = add 2 2
let result2 = add 5 2

AssertEquality result1 __
AssertEquality result2 __

//---------------------------------------------------------------

// ---- nesting functions ---------------------------------------

let quadruple x =    
    let double x =
        x * 2

    double(double(x))

let result = quadruple 4
AssertEquality result __

//---------------------------------------------------------------

// ---- adding type annotations ---------------------------------

(* Sometimes you need to help F#'s type inference system out with
   an explicit type annotation *)

let sayItLikeAnAuctioneer (text:string) =
    text.Replace(" ", "")

let auctioneered = sayItLikeAnAuctioneer "going once going twice sold to the lady in red"
AssertEquality auctioneered __

//TRY IT: What happens if you remove the type annotation on text?

//---------------------------------------------------------------

// ---- using parenthesis to control the order of operation -----

let add x y =
    x + y

let result = add (add 5 8) (add 1 1)

AssertEquality result __

(* TRY IT: What happens if you remove the parensthesis?*)

//---------------------------------------------------------------

//---------------------------------------------------------------
// Tuples
//
// Tuples are used to easily group together values in F#. They're 
// another fundamental construct of the language.
//---------------------------------------------------------------

// ---- creating tuples -----

let items = ("apple", "dog")

AssertEquality items ("apple", __)

//---------------------------------------------------------------
        
// ---- accessing tuple elements --------------------------------

let items = ("apple", "dog", "Mustang")

let fruit, animal, car = items

AssertEquality fruit __
AssertEquality animal __
AssertEquality car __

(* Breaking apart tuples in this way uses a F# feature called 
   pattern matching. Pattern matching is another key concept in 
   F#, and you'll see more examples of it later on. *)

//---------------------------------------------------------------
        
// ---- ignoring values when pattern matching -------------------
       
let items = ("apple", "dog", "Mustang")

let _, animal, _ = items

AssertEquality animal __
    
//---------------------------------------------------------------
        
// ---- using tuples to return multiple values from a function --

let squareAndCube x =
    (x ** 2.0, x ** 3.0)

let squared, cubed = squareAndCube 3.0


AssertEquality squared __
AssertEquality cubed __
    
//---------------------------------------------------------------

//---------------------------------------------------------------
// Branching
//
// Branching is used to tell a program to conditionally perform
// an operation. It's another fundamental part of F#.
//---------------------------------------------------------------

// ---- basic if statements -------------------------------------

let isEven x =
    if x % 2 = 0 then
        "it's even!"
    else
        "it's odd!"
        
let result = isEven 2                
AssertEquality result __

//---------------------------------------------------------------

// ---- if statements return values -----------------------------
    
(* If you've worked with other programming languages, you might
   be surprised to find out that if statements in F# return
   values. *)
   
let result = 
    if 2 = 3 then
        "something is REALLY wrong"
    else
        "math is workng!"

AssertEquality result __

//---------------------------------------------------------------

// ---- branching with pattern matching -------------------------
 
let isApple x =
    match x with
    | "apple" -> true
    | _ -> false

let result1 = isApple "apple"
let result2 = isApple ""

AssertEquality result1 __
AssertEquality result2 __

//---------------------------------------------------------------

// ---- branching with tuples and pattern matching --------------
    
let getDinner x =
    match x with
    | (name, "veggies")
    | (name, "fish")
    | (name, "chicken") -> sprintf "%s doesn't want red meat" name
    | (name, foodChoice) -> sprintf "%s wants 'em some %s" name foodChoice 
    
let person1 = ("Bob", "fish")
let person2 = ("Sally", "Burger")

AssertEquality (getDinner person1) __
AssertEquality (getDinner person2) __

//---------------------------------------------------------------

//---------------------------------------------------------------
// Lists
//
// Lists are a another important building block to use in F# 
// programming. They are used to group an arbitrarily large 
// sequence of values. It's very common to store a values in a 
// list and perform operations across each value in the list.
//---------------------------------------------------------------
   
// ---- creating lists ------------------------------------------

let list = ["apple"; "pear"; "grape"; "peach"]

AssertEquality list.Head __
AssertEquality list.Tail __
AssertEquality list.Length __

//---------------------------------------------------------------

// ---- building new lists---------------------------------------

let first = ["grape"; "peach"]
let second = "pear" :: first
let third = "apple" :: second

//Note: "::" is known as "cons"

AssertEquality ["apple"; "pear"; "grape"; "peach"] third
AssertEquality second __
AssertEquality first __

//What happens if you uncomment the following?

//first.Head <- "apple"
//first.Tail <- ["peach"; "pear"]

//THINK ABOUT IT: Can you change the contents of a list once it 
//                has been created?

//---------------------------------------------------------------

// ---- creating lists with a range------------------------------

let list = [0..4]

AssertEquality list.Head __
AssertEquality list.Tail __

//---------------------------------------------------------------

// ---- creating lists with comprehensions-----------------------

let list = [for i in 0..4 do yield i ]
                            
AssertEquality list __

//---------------------------------------------------------------
    
// ---- comprehensions with conditions --------------------------

let list = [for i in 0..10 do 
                if i % 2 = 0 then yield i ]
                    
AssertEquality list __

//---------------------------------------------------------------
    
// ---- transforming lits with map ------------------------------

let square x =
    x * x

let original = [0..5]
let result = List.map square original

AssertEquality original __
AssertEquality result __

//---------------------------------------------------------------
    
// ---- filtering lists -----------------------------------------
 
let isEven x =
    x % 2 = 0

let original = [0..5]
let result = List.filter isEven original

AssertEquality original __
AssertEquality result __

//---------------------------------------------------------------

// ---- dividing lists with partition ---------------------------

let isOdd x =
    not(x % 2 = 0)

let original = [0..5]
let result1, result2 = List.partition isOdd original

AssertEquality result1 __
AssertEquality result2 __

(* Note: There are many other useful methods in the List module. Check them
   via intellisense in Visual Studio by typing '.' after List, or online at
   http://msdn.microsoft.com/en-us/library/ee353738.aspx *)

//---------------------------------------------------------------

//---------------------------------------------------------------
// Pipelining
//
// Now that you've seen a few operations for working with lists, 
// you can  combine them to do more interesting things
//---------------------------------------------------------------


// ---- squaring even numbers with separate statements ----------

let square x =
    x * x

let isEven x =
    x % 2 = 0

(* One way to combine the operations is by using separate statements.
   However, this is can be clumsy since you have to name each result. *)

let numbers = [0..5]

let evens = List.filter isEven numbers
let result = List.map square evens

AssertEquality result __

//---------------------------------------------------------------

// ---- squaring even numbers with parens -----------------------

(* You can avoid this problem by using parens to pass the result of one
   funciton to another. This can be difficult to read since you have to 
   start from the innermost function and work your way out. *)

let numbers = [0..5]

let result = List.map square (List.filter isEven numbers)

AssertEquality result __

//---------------------------------------------------------------

// ---- squaring even numbers with the pipeline operator --------

(* In F#, you can use the pipeline operator to get the benefit of the 
   parens style with the readablity of the statement style. *)

let result =
    [0..5]
    |> List.filter isEven
    |> List.map square

AssertEquality result __

//---------------------------------------------------------------

// ---- using lambdas -------------------------------------------

let colors = ["maize"; "blue"]

let echo = 
    colors
    |> List.map (fun x -> x + " " + x)

AssertEquality echo __

(* The fun keyword allows you to create a function inline without giving
   it a name. These functions are known as anonymous functions, lambdas,
   or lambda functions. *)

//---------------------------------------------------------------
val expected_value : int

Full name: Script.expected_value
val actual_value : obj

Full name: Script.actual_value
val x : int

Full name: Script.x
val typeOfX : System.Type

Full name: Script.typeOfX
System.Object.GetType() : System.Type
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val y : string

Full name: Script.y
val expectedType : System.Type

Full name: Script.expectedType
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val typeOfY : System.Type

Full name: Script.typeOfY
val mutable x : int

Full name: Script.x
val add : x:int -> y:int -> int

Full name: Script.add
val x : int
val y : int
val result1 : int

Full name: Script.result1
val result2 : int

Full name: Script.result2
val quadruple : x:int -> int

Full name: Script.quadruple
Multiple items
val double : (int -> int)

--------------------
type double = System.Double

Full name: Microsoft.FSharp.Core.double
val result : int

Full name: Script.result
val sayItLikeAnAuctioneer : text:string -> string

Full name: Script.sayItLikeAnAuctioneer
val text : string
System.String.Replace(oldValue: string, newValue: string) : string
System.String.Replace(oldChar: char, newChar: char) : string
val auctioneered : string

Full name: Script.auctioneered
val items : string * string

Full name: Script.items
val items : string * string * string

Full name: Script.items
val fruit : string

Full name: Script.fruit
val animal : string

Full name: Script.animal
val car : string

Full name: Script.car
val squareAndCube : x:float -> float * float

Full name: Script.squareAndCube
val x : float
val squared : float

Full name: Script.squared
val cubed : float

Full name: Script.cubed
val isEven : x:int -> string

Full name: Script.isEven
val result : string

Full name: Script.result
val isApple : x:string -> bool

Full name: Script.isApple
val x : string
val result1 : bool

Full name: Script.result1
val result2 : bool

Full name: Script.result2
val getDinner : string * string -> string

Full name: Script.getDinner
val x : string * string
val name : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val foodChoice : string
val person1 : string * string

Full name: Script.person1
val person2 : string * string

Full name: Script.person2
Multiple items
val list : string list

Full name: Script.list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
property List.Head: string
property List.Tail: string list
property List.Length: int
val first : string list

Full name: Script.first
val second : string list

Full name: Script.second
val third : string list

Full name: Script.third
Multiple items
val list : int list

Full name: Script.list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
property List.Head: int
property List.Tail: int list
val i : int
val square : x:int -> int

Full name: Script.square
val original : int list

Full name: Script.original
val result : int list

Full name: Script.result
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val isEven : x:int -> bool

Full name: Script.isEven
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val isOdd : x:int -> bool

Full name: Script.isOdd
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val result1 : int list

Full name: Script.result1
val result2 : int list

Full name: Script.result2
val partition : predicate:('T -> bool) -> list:'T list -> 'T list * 'T list

Full name: Microsoft.FSharp.Collections.List.partition
val numbers : int list

Full name: Script.numbers
val evens : int list

Full name: Script.evens
val colors : string list

Full name: Script.colors
val echo : string list

Full name: Script.echo
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/bG
Posted:12 years ago
Author:Chris Marinos
Tags: f# koans