<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Functional programming interview question</title>
	<atom:link href="http://www.fsharp.it/2009/08/26/functional-programming-interview-question/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fsharp.it/2009/08/26/functional-programming-interview-question/</link>
	<description>Functional programming on .Net</description>
	<lastBuildDate>Wed, 18 Aug 2010 12:04:06 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: lionel</title>
		<link>http://www.fsharp.it/2009/08/26/functional-programming-interview-question/comment-page-1/#comment-7168</link>
		<dc:creator>lionel</dc:creator>
		<pubDate>Thu, 27 Aug 2009 08:20:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=167#comment-7168</guid>
		<description>I think you can make that simpler still by using a Seq.fold and letting that do the parsing of the state down the sequence. That way you can get rid of all the list processing stuff from you function. The trick to do grouping is cool. 

[fsharp]
type lineType = ANIMALS &#124; NUMBERS

let processLine (state, animals, numbers) line = 
    match line, state with
    &#124; &quot;ANIMALS&quot;, _ -&gt; (ANIMALS, animals, numbers)
    &#124; &quot;NUMBERS&quot;, _ -&gt; (NUMBERS, animals, numbers)
    &#124; _, ANIMALS -&gt; (state, line::animals, numbers)
    &#124; _, NUMBERS -&gt; (state, animals, line::numbers)
    
let (_, animals, numbers) = 
    System.IO.File.ReadAllLines(&quot;input.txt&quot;) &#124;&gt; Seq.fold processLine (ANIMALS, [], [])

let groupList = Set.of_list &gt;&gt; Set.to_list
let sortedAnimals = animals &#124;&gt; groupList &#124;&gt; List.sort
let sortedNumbers = numbers &#124;&gt; groupList &#124;&gt; List.sort

printf &quot;There are %d animals which are: %A\r\n&quot; (List.length sortedAnimals) sortedAnimals
printf &quot;There are %d numbers which are: %A\r\n&quot; (List.length sortedNumbers) sortedNumbers

System.Console.ReadLine() &#124;&gt; ignore
[/fsharp]</description>
		<content:encoded><![CDATA[<p>I think you can make that simpler still by using a Seq.fold and letting that do the parsing of the state down the sequence. That way you can get rid of all the list processing stuff from you function. The trick to do grouping is cool. </p>
<pre class="brush: fsharp">
type lineType = ANIMALS | NUMBERS

let processLine (state, animals, numbers) line =
    match line, state with
    | &quot;ANIMALS&quot;, _ -&gt; (ANIMALS, animals, numbers)
    | &quot;NUMBERS&quot;, _ -&gt; (NUMBERS, animals, numbers)
    | _, ANIMALS -&gt; (state, line::animals, numbers)
    | _, NUMBERS -&gt; (state, animals, line::numbers)

let (_, animals, numbers) =
    System.IO.File.ReadAllLines(&quot;input.txt&quot;) |&gt; Seq.fold processLine (ANIMALS, [], [])

let groupList = Set.of_list &gt;&gt; Set.to_list
let sortedAnimals = animals |&gt; groupList |&gt; List.sort
let sortedNumbers = numbers |&gt; groupList |&gt; List.sort

printf &quot;There are %d animals which are: %A\r\n&quot; (List.length sortedAnimals) sortedAnimals
printf &quot;There are %d numbers which are: %A\r\n&quot; (List.length sortedNumbers) sortedNumbers

System.Console.ReadLine() |&gt; ignore
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
