Questions tagged [f#]
F# is a succinct, expressive, and efficient functional and object-oriented language for .NET
357
questions
25
votes
2
answers
2k
views
First real-world F# application - how "good"/idiomatic is it? (long!)
After reading/watching various introductions and blog posts and some code here and there, doing a bit of the Try F# tutorial and starting to read "Real World Functional Programming", I felt I should ...
16
votes
0
answers
190
views
Encoding the problem domain of products and prices into the typesystem in F#
I'm trying to design a model for purchasing amounts of products in F#, following the ideas for designing for correctness (source). The idea is to use the type system to capture the different legal ...
15
votes
4
answers
1k
views
Project Euler 6: Difference between sum of squares and square of sum
I've created a (very) simple solution for Project Euler problem 6:
Project Euler Problem 6: Sum square difference
The sum of the squares of the first ten natural numbers is,
$$ 1^2 + 2^2 +...
14
votes
1
answer
570
views
Grand Chess domain model and helper functions
So I am trying to write, essentially from a blank slate, a program that plays Grand Chess. In short, it is a chess variant that is played with two extra pieces, on a 10x10 board, no castling, and ...
13
votes
3
answers
967
views
Return total balance from unsorted purchase items that may have specials
This is in reference to the following kata:
...
13
votes
3
answers
1k
views
Poker Hands Kata in F#
I'm a F# newbie, and would like to share my implementation of the Poker Hands Kata problem to get some feedback.
...
12
votes
2
answers
11k
views
Reading input from console in F# (as a sequence of lines)
Consuming input line-by-line until the end is often useful. In C# I would write the following loop:
while ((line = Console.ReadLine()) != null) {
// ...
I ...
11
votes
1
answer
491
views
American Checkers
I am implementing the logic for a Checkers game in F#. I am writing my code in a library so it can be called from any UI provider, and am trying to do it in good FP style. I currently have the ...
11
votes
1
answer
589
views
Ugly Numbers: A Rags-to-Riches Story
I stumbled upon an unaswered question, which looked like a good fit for a functional programming language.
Here is the problem statement from codeeval:
UGLY NUMBERS
CHALLENGE DESCRIPTION:
...
11
votes
1
answer
921
views
Approach to programmatically building hierarchical GUI components
At work I am developing an application using hand-coded Swing, and I've found that I have an easier time reading, writing, and maintaining hierarchical component creation using code blocks like:
<...
10
votes
4
answers
5k
views
Functional approach to splitting list into sub lists
Here is my functional approach to split a list into sub lists, each having non-decreasing numbers, so the input
\$[1; 2; 3; 2; 4; 1; 5;]\$
computes to
\$[[1; 2; 3]; [2; 4]; [1; 5]]\$
The code ...
10
votes
2
answers
386
views
You can't sink me
Here is a Battlehips game in F#. Now, before you start jumping up and down on my code, please understand that, for all its awesomeness, F# has a serious limitation: Its compilation is linear, and the ...
10
votes
1
answer
528
views
Getting the last date where a given week day occurred
I'm trying to learn a little bit about functional programming and as my tool I chose F# since I'm a .NET developer and the environment is more natural to me.
In one of my pet projects I'm dealing ...
10
votes
2
answers
242
views
Is this Bridges code F# idiomatic?
Introduction
I'm primarily a C# programmer, just starting out with learning F#. I found myself with a problem which felt like it was appropriate for a functional language, and now that I have the ...
10
votes
2
answers
367
views
F#zzBuzz: Learning the easy way
So I made a quick programme in F# to do FizzBuzz, I'm trying to learn more languages, so I figured I'd do something functional for a change. (Haskell is also on the list.)
FizzBuzz is pretty self-...
10
votes
3
answers
175
views
Generating image gauges from a value, min and max
One of the things I was recently investigating was the ability to generate "gauge" charts—that is, a chart which shows where a value stands within a range of values.
As an example, if we know ...
10
votes
1
answer
970
views
Small game written in F#
I'm trying to wrap my head around F#. I'd like your honest opinion about this simple game source code that I wrote. Rules are simple:
Every player has soldiers and territory
Player can recruit ...
10
votes
2
answers
695
views
American Checkers with AI
This is the third question in the series. Number 1 had most of the official two-player rules implemented, and Number 2 was the basic UI. This one has the complete two-player rules implemented, an AI ...
10
votes
2
answers
235
views
F# inventory system - Part 2
I've refactored my previous inventory system, and added a few features like removing items from the Inventory class, easily obtaining the current selected item ...
9
votes
1
answer
4k
views
Reading input from the console in F#
In the process of learning F#, I wrote this code that reads lines of input from the console and stores them in a list.
As far as I can tell the code works correctly, but since I'm new to functional ...
9
votes
2
answers
256
views
Unit testing F# code without using an existing test library (MSTest, xUnit, etc.)
I know I can probably use MSTest (I'm under Linux without an IDE so maybe not?) or another unit testing library, but for a small project I decided to write my own unit tests without resorting to a ...
9
votes
1
answer
792
views
Prime factorization in F#
In the pursuit of learning F#, I have been working through some Project Euler problems.
This is my solution for problem 3:
...
9
votes
2
answers
1k
views
Hierarchical State Machine in F#
I appreciate any feedback on where I might alter or improve my code for this project. This is an old attempt at implementing a Hierarchical State Machine in F#. I'm from a C#/OO background mostly.
<...
9
votes
1
answer
746
views
Deleting from Red Black Tree in F#
Yes I'm very slowly making my way through Purely Functional Data Structures. So I went through the section on Red Black Trees. What he presents is amazingly concise, except for the fact that he didn't ...
9
votes
1
answer
317
views
Brainfuck interpreter in F#
I have some concerns, like the updateValue function. I was trying to follow the functional paradigm, but I wonder if I could use another approach or something.
My ...
8
votes
4
answers
7k
views
Swap even/odd characters
I wrote some code to solve this problem https://www.hackerrank.com/challenges/string-o-permute which basically states: take an even length string and swap all the even indexed characters. For example ...
8
votes
2
answers
1k
views
Mock/Stub out filesystem in F# for unit testing
I'm looking to do some basic verification testing on my functions that write to the filesystem.
I took a hint from here on how to mock out the filesystem using an interface, but I'm kinda bummed on ...
8
votes
2
answers
143
views
Wordpress RSS Parser
Follow Up to my earlier question.
This is an RSS Parser intended only to parse the feed Wordpress provides (and even then, only what I actually want to display on my website). ...
8
votes
2
answers
726
views
Caesar cipher in F#
I'm trying to broaden my horizon by learning functional programming (coming from OO, specficially C#). For this, I'm implementing some small problems to get a feeling for the language. One of my first ...
8
votes
1
answer
1k
views
Generic build script using Fake
I have something like a generic build script for a group of projects. I am using Fake but my F# skills are pretty bad. Would you please take a look and point where I can improve it?
ORIGINAL
...
8
votes
1
answer
577
views
F# Either computation expression with while loop
I want to implement an Either computation expression in F#. The computation executes a function inside a while loop, and this function's return type is ...
8
votes
1
answer
974
views
Simple RabbitMQ client wrapper
After reading this question, I've realized that I can do a lot to improve the quality of my question, so I've edited this question quite a bit.
I've been teaching myself F# in my spare time off and ...
8
votes
1
answer
1k
views
Discriminated-unions in C#
So I really want to have something similar to discriminated unions in C#.
One way to do it is to use a visitor pattern, but it takes half a life to write all broilerplate code by hands.
There is ...
8
votes
2
answers
312
views
Conway's Game of Life F#
I am a C# developer learning F#. Would appreciate any suggestions on how this code can be improved. Main points I am interested in:
Functional style of the code, absense of imperative style(C#) ...
7
votes
3
answers
170
views
Doing an ROP style bind for two functions on the same input in a pipeline
I am working on understanding Railway Oriented Programming (Scott Wlaschin style) in F#. In my example I want to create a pipeline which does some calculation, applies two different functions to the ...
7
votes
3
answers
353
views
Call an F# function with its previous result until the output no longer changes
Basically I have a function of the type 'a -> 'a (an optimization function on a AST) and I want to call it (passing the previous result) until it returns the ...
7
votes
3
answers
2k
views
Splitting a sequence into equal segments
I need to split up a sequence into equal segments of a given size (yes, the last one may be shorter), and I'm trying to find an efficient and idiomatic way to do it.
I have two versions of the ...
7
votes
3
answers
353
views
Functional Programming style in F#
I'm normally a C# developer, but I've started to learn F# and I want to make sure I'm writing code in a functional way that suits the language. I've quickly pieced this together with my knowledge from ...
7
votes
1
answer
1k
views
Map implementation in F#
Just as a refresher I put together a simple Map implementation and I would love to get some feedback on it.
...
7
votes
1
answer
574
views
F# inventory system
To learn F#, I've implemented this very simple inventory system. While I'm proud that that it's my first program, and that it works, there are still a few areas that I'd like tips on, namely these:
I ...
7
votes
1
answer
215
views
Small console app to execute some remote scripts
I'm learning F# and functional programming, from a background in C# and imperative/OOP. I've ported a small, one-off console app from C# to F#. The port worked (the app behaves the same way), but I'd ...
7
votes
1
answer
1k
views
F# Djikstras shortest path implementation
I'm just getting my feet wet with F#, by implementing some simple algorithms. First up: Djikstras shortest path.
There is one piece I've written in this code which confuses me as to why it works: the ...
7
votes
1
answer
182
views
Refactoring of two command handler implementations with F# in the context of event sourcing
I've written some F# code to handle commands in the context of event sourcing which works along the lines below:
Read Events from a specific event store stream
Build the current state in the given ...
7
votes
1
answer
121
views
World's worst Christmas tree
Inspired by this question, I decided to grow my own fractal tree.
The problem is: given an integer \$n\$, \$0 \leq n \leq 5\$, print the \$n\$th iteration of the fractal tree. The tree is probably ...
7
votes
1
answer
217
views
TCPListener server to discover clients on a network
I am currently writing a program to sync files (music, photos, etc) from my PC to an Android device. In order to do this, I have 2 application: one that is running on my PC, and one that shall be ...
7
votes
1
answer
105
views
Streaming a Pin functionally(ish)
Earlier today I wrote a question (Validating a StreamingPin) and I thought it was a good idea (apparently I was the only one who thought so, but whatever), so I proceeded further with it and developed ...
7
votes
1
answer
592
views
Efficient generic type conversion between numeric types in F#
It's easy to write a function that adds two ints in F#:
let add x y = x + y
Actually, it's the same as:
...
7
votes
0
answers
116
views
Agent-based Immutable Map
I am in the process of removing the last few pieces of mutable state from an F#-based distributed system. Some of the remaining mutable state is a ...
7
votes
0
answers
531
views
A* Algorithm in F#
Inspired by this post I looked up A* on wikipedia and went on with my own implementation as seen below where I try to mimic the pseudocode on Wikipedia but in a recursive manner.
I would like any ...
6
votes
3
answers
494
views
Is there any way to improve (shorten) this F# code?
I have a very good grasp of the syntax and features of F# as well as some of the concepts that mesh well with the language. However, I do not have enough experience writing it to feel comfortable that ...