Snippets in category Learning F#

  • Form changing color

    Windows Forms tutorial showing how to create form with button and how to register an event handler. When the button is clicked, the form changes its background color.

    29 people like this
    Posted: 1 years ago by Tomas Petricek

  • Hello world (F#)

    Classical "Hello world" example that prints a message to the console output. This version uses F# printfn function to do the printing.

    131 people like this
    Posted: 1 years ago by Tomas Petricek

  • Hello world (.NET)

    Classical "Hello world" example that prints a message to the console output. This version uses .NET Console.WriteLine method to do the printing.

    23 people like this
    Posted: 1 years ago by Tomas Petricek

  • Largest Palindrome Number from Product of Two Three Digit Numbers

    Here is an improved version twice shorter, than original

    35 people like this
    Posted: 1 years ago by Nick Canzoneri

  • Project Euler #1

    This snippet is code that solves first Project Euler problem. It finds the sum of all the multiples of 3 or 5 below 1000. Please add other (more efficient, succinct or interesting) solutions to this snippet.

    37 people like this
    Posted: 1 years ago by Eugene Gavrin

  • Project Euler #2

    Solution to Project Euler problem 2: Find sum of even terms in fibonacci sequence which do not exceed four million. Comments about the first version: 1. It does't use memoize becouse of recursive call to unmemoized function "fibs". So it take over 20 sec to get answer. A minor change ("fibs" to "fibs' ") reduces this time to 140ms.

    25 people like this
    Posted: 1 years ago by D

  • Project Euler #182

    The RSA encryption is based on the following procedure: Generate two distinct primes p and q. Compute n=pq and phi=(p-1)(q-1). Find an integer e, 1<e<phi, such that gcd(e,phi)=1. There exist values of e and m such that m^(e) mod n=m. We call messages m for which m^(e) mod n=m unconcealed messages. Choose p=1009 and q=3643. Find the sum of all values of e, so that the number of unconcealed messages for this value of e is at a minimum.

    20 people like this
    Posted: 1 years ago by Natallie Baikevich

  • Pascal's Triangle

    This code sample generates Pascal's triangle as jagged 2D list (list of lists).

    13 people like this
    Posted: 1 years ago by Dmitry Soshnikov

  • Pascal's Triangle (2)

    This code sample generates Pascal's triangle as jagged 2D list (list of lists). It takes 0.5 sec to generate 3000 rows (vs 16 sec for http://fssnip.net/23). Tip: try to avoid of using "list1 @ list2" construction.

    8 people like this
    Posted: 1 years ago by Shamil Sayfutdinov

  • Functional wrappers for TryParse APIs

    Exemplary convenience wrappers for some of the System.<Typename>.TryParse APIs, using the combined power of F#' return value deconstruction mechanism via pattern matching, active patterns and option types instead of "out/ref" parameters

    15 people like this
    Posted: 1 years ago by Novox

  • Re-creating arithmetic with DU

    You never know when you might need this.

    7 people like this
    Posted: 1 years ago by Dmitri Pavlenkov

  • Scheme interpreter in F#

    A small Scheme interpreter using Higher Order Abstract Syntax (HOAS) encoding for terms. The essence of the technique is to use F# (meta-level) functions to encode Scheme (object-level) functions and other binding constructs, thus avoiding the need for representing variables, bindings, explicit substitution and dealing with shadowing.

    17 people like this
    Posted: 1 years ago by Anton Tayanovskyy

  • Wicked way to solve quadratic equation using list of operators

    This is to demonstrate that: (1) there are many ways to solve the same problems; (2) operators can be grouped together into data structures and act as data; (3) you can have fun in F# in many ways.

    49 people like this
    Posted: 1 years ago by Dmitry Soshnikov

  • Primitive Pythagorean triples

    Primitive Pythagorean triples generator. It uses an Algorithm found on Wolfram MathWorld and the F# PowerPack matrix library.

    31 people like this
    Posted: 1 years ago by Cesar Mendoza

  • Object oriented as it was supposed to be ?

    The snippet shows implementing object orientation using mail box processor. In this context object orientation have this simple definition: "Objects acts on message passing". The objects created this way are thread safe too :). Not sure how much practical this would be in todays context where object oriented has gone the wrong way.

    19 people like this
    Posted: 1 years ago by Ankur Dhama

  • Nullable to Option

    it's allways a pain to work with F#'s Option values outside of F# - there you've got the Nullable-class this is a short snippet to convert Nullable<'a> to 'a option

    2 people like this
    Posted: 1 years ago by Carsten König

  • Combinatorial functions

    Here is my F# take on some combinatorial functions from the book "Introduction to Functional Programming" by Richard Bird and Philip Wadler.

    5 people like this
    Posted: 1 years ago by Cesar Mendoza

  • Solution to the problem mentioned at : http://professor-fish.blogspot.com/2011/01/tiny-bit-of-denotational-semantics.html

    Solution to the problem mentioned at : http://professor-fish.blogspot.com/2011/01/tiny-bit-of-denotational-semantics.html

    1 people like this
    Posted: 1 years ago by Ankur Dhama

  • Wrapped Class/Interface

    Wrapped Class/Interface Remark: better this way - see example

    0 people like this
    Posted: 1 years ago by fholm

  • Active pattern to define default values

    This active pattern allows you to specify default values for option arguments in the signature of your function, so you can remove unnecessary calls to defaultArg. It also save you having to define a new name for the defaulted value.

    7 people like this
    Posted: 11 months ago by Kurt Schelfthout

  • Functions not so first class in active patterns

    It seems that you can't pass anonymous functions as parameters to active patterns.

    3 people like this
    Posted: 11 months ago by Kurt Schelfthout

  • escaping string

    escaping string, very terse . Update: a bit more terse, and perf improvement by using .ToCharArray() and Array.iter

    1 people like this
    Posted: 11 months ago by hammett

  • Tetris

    Playable Tetris mini-game. Use arrow keys to move left and right and up to rotate, down to drop. Try it out in the browser on TryFSharp.org

    10 people like this
    Posted: 10 months ago by Phillip Trelford

  • Wimbledon special!

    Tennis scoring system (at the game level). Includes some pattern-matching examples including 'when' guards.

    4 people like this
    Posted: 10 months ago by Kit Eason

  • Finding matching pairs in two sequences

    Here's a function which takes a comparator function and two sequences, and returns tuples consisting of an item from each sequence, where the comparator function returns true for those two items. This is a small part of my wider project to generate guitar chord shapes. One of the requirements there is to take a list of 'wanted' notes for a chord, and a list of 'available' notes within a range of frets and to combine them into an actual set of frettings. That's what led to tryFindFirstPair and hence to findPairs.

    1 people like this
    Posted: 9 months ago by Kit Eason

  • Euler #5

    Euler #5 solution

    3 people like this
    Posted: 8 months ago by Michael Falanga

  • Minesweeper in 99 lines of code

    This program is written in only 99 lines of actual code and remains enough readability. I used few short-coding technics. 1. no XAML. 2. pre-calculate every useful data for the purpose of eliminating useless states 3. using record type with set property as an alternative of view-model 4. initialize everything in one place. 5. encapsulate all states in one place.

    14 people like this
    Posted: 8 months ago by nagat01

  • Project Euler Problem 19

    How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

    2 people like this
    Posted: 8 months ago by Gene Belitski

  • Project Euler Problem 31

    In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p How many different ways can £2 be made using any number of coins?

    3 people like this
    Posted: 8 months ago by Gene Belitski

  • Gluing-up sequence members

    While thinking on Project Euler Problem 40 solution (http://projecteuler.net/index.php?section=problems&id=40) the following subproblem has surfaced: how to glue up string presentations of a sequence members to reach a given length of result string. The snippet gives at least 3 different implementations of such function with performance comparison; as a bonus a solution to Problem 40 is given.

    2 people like this
    Posted: 8 months ago by Gene Belitski

  • Times Table game

    Simple times table console based game.

    2 people like this
    Posted: 7 months ago by Phillip Trelford

  • Another tennis implementation

    Simple game of tennis... refactored after reading Richard Minerich blog post @http://richardminerich.com/2011/02/the-road-to-functional-programming-in-f-from-imperative-to-computation-expressions/

    2 people like this
    Posted: 6 months ago by Colin Bull

  • Yet another Tennis Kata

    It's a joke one liner implementation of Tennis Kata.Please see this code not seriously.

    1 people like this
    Posted: 6 months ago by nagat01

  • toString and fromString for discriminated unions

    General toString and fromString for discriminated unions using FSharp.Reflection

    16 people like this
    Posted: 5 months ago by Jonas Avelin

  • DSL for financial contracts

    Simple domain-specific language (DSL) for describing financial contracts in F#. A contract is represented using a discriminated union. Evaluating a contract gives the orders that may happen at a given date.

    6 people like this
    Posted: 5 months ago by Tomas Petricek

  • Baccarat

    Shuffles a deck of cards, deals 2 hands applying punto banco rules before declaring a winner or a tie.

    4 people like this
    Posted: 4 months ago by Phillip Trelford

  • PacMan Maze

    PacMan maze view runnable inside TryFSharp.org

    4 people like this
    Posted: 4 months ago by Phillip Trelford

  • Pong

    Pong video game runnable inside TryFSharp.org. Player 1 keys 'Q' - up, 'A' - down. Player 2 keys 'P' - up, 'L' - down.

    5 people like this
    Posted: 3 months ago by Phillip Trelford

  • Ninety-Nine F# Problems - Problems 1 - 10 - Lists

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    4 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 11 - 20 - List, continued]

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 21 - 28 - Lists again

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 31 - 41 - Arithmetic]

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 46 - 50 - Logic and Codes

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 54 - 60 - Binary trees

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    2 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 61 - 69 - Binary trees

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    2 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 70 - 73 - Multiway Trees

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 80 - 89 - Graphs

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 90 - 94 - Miscellaneous problems]

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    2 people like this
    Posted: 3 months ago by Cesar Mendoza

  • Ninety-Nine F# Problems - Problems 95 - 99 - Miscellaneous problems]

    These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

    3 people like this
    Posted: 3 months ago by Cesar Mendoza

  • TicTacToe(Joinads Example)

    TicTacToe game simulator implemented by using Joinads(http://tomasp.net/blog/joinads-async-prog.aspx). Game logic in this snippet was simplified so nicely by using Joinads. You can run this snippet on Try Joinads (http://tryjoinads.org/).

    3 people like this
    Posted: 3 months ago by nagat01

  • Langton's ant

    Implementation of Langton's ant route.. Takes first 1000 steps and returns only black fields.

    1 people like this
    Posted: 2 months ago by stejcz

  • Langton's ant // OOP :)

    Implementation of Langton's ant route.. Takes first 1000 steps and returns only black fields.

    3 people like this
    Posted: 2 months ago by stejcz

  • HTML File Type

    Discriminated unions to represent a HTML file.(not completely)

    2 people like this
    Posted: 1 months ago by Gab_km

  • What word is most like the word "turtle"?

    A while ago I posted a snippet to calculate the 'Discrete Fréchet Distance' between two curves. If we treat a word as a 'curve' by giving each letter an index (with similar-sounding letters having closer indices) we can compare words by the Fréchet distance between them! An alternative to edit-distance...

    2 people like this
    Posted: 1 months ago by Kit Eason

  • Bayesian Monte Carlo of Let's Make a Deal

    This code illustrates Bayes' Theorem in action on the Let's Make a Deal problem, which several authors have used to illustrate Bayes' Theorem. (It's easy to search the internet for further explanation.) Run with the audit option to audit up to the first 100 games. Running without audit is faster and can simulate a couple billion games.

    3 people like this
    Posted: 1 months ago by Jack Fox

  • Tesco in 70 lines of code

    Domain model for the Tesco checkout implemented in F# using discriminated unions (in 20 lines of code) and console-based user interface for scanning products and calculating the total price.

    3 people like this
    Posted: 1 months ago by Tomas Petricek

  • Finalizing Tesco purchase

    The sample shows two different reprezentations of Tesco checkout. The first one stores scanned items - as a list of either purchase or cancel items - and the second stores final bill with product and total quantity. The snippet implements transformation that corresponds to finalizing the purchase.

    3 people like this
    Posted: 1 months ago by Tomas Petricek