<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">So long, and thanks for all the F#</title><subtitle type="html" /><id>http://cs.hubfs.net/blogs/solong/atom.aspx</id><link rel="alternate" type="text/html" href="http://cs.hubfs.net/blogs/solong/default.aspx" /><link rel="self" type="application/atom+xml" href="http://cs.hubfs.net/blogs/solong/atom.aspx" /><generator uri="http://communityserver.org" version="2.0.60217.2664">Community Server</generator><updated>2006-06-21T08:15:00Z</updated><entry><title>Some notes about using F# in ProjectEuler</title><link rel="alternate" type="text/html" href="http://cs.hubfs.net/blogs/solong/archive/2008/01/14/4561.aspx" /><id>http://cs.hubfs.net/blogs/solong/archive/2008/01/14/4561.aspx</id><published>2008-01-15T05:20:00Z</published><updated>2008-01-15T05:20:00Z</updated><content type="html">&lt;P&gt;(After a long hiatus of just lurking and posting to the forums, I am trying to start blogging again.)&lt;/P&gt;
&lt;P&gt;Like many people, I recently discovered &lt;A href="http://projecteuler.net/"&gt;http://projecteuler.net&lt;/A&gt;.&amp;nbsp; The site contains a set of mathematically oriented programming problems that get progressively harder.&amp;nbsp; I started solving them using F#.&amp;nbsp; While doing so, I've come up with some comments on how F# is really good for these kinds of problems and some improvements that would be nice and probably applicable to more general kinds of programs.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;BigInt rules!&amp;nbsp; Since a lot of the problems are calculations on large integers, it is nice to have them available.&amp;nbsp; It would be nice if there was a "bigint" equivalent to the "int" and "float" functions to simplify conversions.&amp;nbsp; It would be really nice if there was a way to override the default numeric behavior of assuming that every number is an int;&amp;nbsp; it would be nice to be able to switch to "float" or "bigint" mode and have the type checker make the appropriate assumptions. 
&lt;LI&gt;A minor nit, but it would be nice to have a predefined identity function.&amp;nbsp; I seem to have one in each program I write (even beyond ProjectEuler). 
&lt;LI&gt;For most of the problems, I'd&amp;nbsp;create a sequence expression with individual calculations and then aggregate the desired result after filtering.&amp;nbsp; This demonstrated the convenience of sequence comprehensions, but we already knew that :-) 
&lt;LI&gt;I tried to avoid "normal" imperative for and while loops and created new helper functions to work on sequences:&amp;nbsp; seqUntil and seqWhile would process a sequence depending on a boolean function, seqAll would see if a function was true for all values in a sequence (the counterpart to Seq.exists), seqSkip would remove the first n entries in a sequence (really creative name, there :-),&amp;nbsp; etc.&amp;nbsp; I think that a lot of these functions, especially the ones that are in the Haskell Prelude for lists, would be good additions to the Seq module. 
&lt;LI&gt;It would be nice to have&amp;nbsp;some of the&amp;nbsp;aggregation operations be representable in the comprehension language.&amp;nbsp; This has been described Simon Peyton-Jones and Phil Wadler in &lt;A href="http://research.microsoft.com/Users/simonpj/papers/list-comp/index.htm"&gt;Comprehensive Comprehensions&lt;/A&gt;&amp;nbsp;for Haskell and should be&amp;nbsp;applicable to F# as well. 
&lt;LI&gt;I've noticed that I mix the sequence comprehension syntax with the "old-style" pipeline syntax a lot, even when the function could be done in a sequence comprehension.&amp;nbsp; Looking back, it seems that if I am exploring (ok, guessing) a solution incrementally, it is easier to do that by adding new Seq.map, Seq.filter, or Seq.fold functions at the end with "|&amp;gt;" while running each step in fsi.&amp;nbsp;&amp;nbsp;Would it help&amp;nbsp;if there was a way to run and augment&amp;nbsp;sequence comprehensions incrementally in fsi?&amp;nbsp; If I knew the path to the solution(or subsolution), I would choose to use the comprehension syntax first. 
&lt;LI&gt;While there seems to have been an interest in having F# be an alternative to Matlab for some kinds of engineering computing, I think that it could give Mathematica a run for its money in the computational and presentation space.&amp;nbsp; Adding full symbolic math support would be nice, but a harder challenge...&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;It has been a fun diversion to work on these problems, and interesting to see how concise the F# solutions are compared to the other languages represented.&amp;nbsp; The solutions in &lt;A href="http://www.jsoftware.com"&gt;J&lt;/A&gt; are always the smallest, but I haven't grokked the syntax to understand the solutions yet :-)&amp;nbsp; The assembly language solutions are always interesting to read, but it is nice to see F# solutions that are as concise as the Ruby and Python solutions but running much faster...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=4561" width="1" height="1"&gt;</content><author><name>LewisBruck</name><uri>http://cs.hubfs.net/members/LewisBruck.aspx</uri></author></entry><entry><title>F# and SQL Server CLR integration (Part 1)</title><link rel="alternate" type="text/html" href="http://cs.hubfs.net/blogs/solong/archive/2006/06/21/378.aspx" /><id>http://cs.hubfs.net/blogs/solong/archive/2006/06/21/378.aspx</id><published>2006-06-21T15:28:00Z</published><updated>2006-06-21T15:28:00Z</updated><content type="html">&lt;P&gt;(reposted from &lt;A href="http://blogs.msdn.com/lbruck/archive/2006/05/25/607029.aspx"&gt;http://blogs.msdn.com/lbruck/archive/2006/05/25/607029.aspx&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;Please read my &lt;A href="/blogs/solong/archive/2006/06/21/377.aspx"&gt;first post&lt;/A&gt; to understand the goals and security implications of the samples I am presenting.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;I will assume that you are in the sysadmin role on your SQL2005 server, you are using a program that allows you to execute TSQL commands (like Management Studio), and that you are running it on the same machine as your F# installation.&amp;nbsp; Change any paths to be appropriate to your machine.&lt;/P&gt;
&lt;P&gt;First of all, you need to turn on CLR support in SQL2005:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;sp_configure &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;'clr enabled'&lt;/SPAN&gt;, 1&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;reconfigure&lt;/SPAN&gt;&lt;BR&gt;go&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Next, you need to create a database and allow the execution of "unsafe" assemblies in it.&amp;nbsp; There is a more sophisticated (and complicated) method involving signing the unsafe assemblies with a key and then granting the appropriate permissions to that key, but for testing purposes I will go the simple route:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;create&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;database&lt;/SPAN&gt; fsharp&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;alter&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;database&lt;/SPAN&gt; fsharp &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;set&lt;/SPAN&gt; trustworthy &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;on&lt;/SPAN&gt;&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;use&lt;/SPAN&gt; fsharp&lt;BR&gt;go&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Now you need to load the two utility libraries used by most F# programs into the database:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;create&lt;/SPAN&gt; assembly fslib &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;from&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;'c:\fsharp\fsharp-1.1.11.7\bin\fslib.dll'&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;with&lt;/SPAN&gt; permission_set = unsafe&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;create&lt;/SPAN&gt; assembly mllib &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;from&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;'c:\fsharp\fsharp-1.1.11.7\bin\mllib.dll'&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;with&lt;/SPAN&gt; permission_set = unsafe&lt;BR&gt;go&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Now you are ready to write your first F# stored procedure.&amp;nbsp; Create a file called sqlclr.fs (the name is important) with following code:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;open&lt;/SPAN&gt; System&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;open&lt;/SPAN&gt; System.Data&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;open&lt;/SPAN&gt; System.Data.Sql&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;open&lt;/SPAN&gt; System.Data.SqlTypes&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;open&lt;/SPAN&gt; Microsoft.SqlServer.Server&lt;BR&gt;&lt;BR&gt;[&amp;lt;SqlProcedure&amp;gt;]&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;let&lt;/SPAN&gt; printToday() =&lt;BR&gt;&amp;nbsp;&amp;nbsp; SqlContext.Pipe.Send(DateTime.Now.ToString())&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Compile this into a DLL.&amp;nbsp; To load and test out the procedure, issue the following commands:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;create&lt;/SPAN&gt; assembly sqlclr &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;from&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;'c:\fsharp\sqlclr\sqlclr.dll'&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;with&lt;/SPAN&gt; permission_set = unsafe&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;create&lt;/SPAN&gt; &lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;procedure&lt;/SPAN&gt; PrintToday external name sqlclr.sqlclr.PrintToday&lt;BR&gt;go&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Lucida Console; BACKGROUND-COLOR: transparent"&gt;exec&lt;/SPAN&gt; PrintToday&lt;BR&gt;go&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I won't go into all of the details of SQL CLR integration at this point, I want to point out some important facts.&amp;nbsp; CLR stored procedures have to be static functions; this works well with normal F# functions since they are defined this way.&amp;nbsp; By default, they get created in a class with the same name as the source file;&amp;nbsp; the name of that class must be used as the second part of the external name of the stored procedure.&lt;/P&gt;
&lt;P&gt;This has been a lot of setup work for very little immediate reward, but it lays the foundation for what I'll present in future posts.&amp;nbsp; Next, I will demonstrate less trivial (and more useful) user-defined functions in F#.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=378" width="1" height="1"&gt;</content><author><name>LewisBruck</name><uri>http://cs.hubfs.net/members/LewisBruck.aspx</uri></author></entry><entry><title>F# and SQL Server CLR integration (Part 0)</title><link rel="alternate" type="text/html" href="http://cs.hubfs.net/blogs/solong/archive/2006/06/21/377.aspx" /><id>http://cs.hubfs.net/blogs/solong/archive/2006/06/21/377.aspx</id><published>2006-06-21T15:15:00Z</published><updated>2006-06-21T15:15:00Z</updated><content type="html">&lt;P&gt;(reposted from &lt;A href="http://blogs.msdn.com/lbruck/archive/2006/05/24/606653.aspx"&gt;http://blogs.msdn.com/lbruck/archive/2006/05/24/606653.aspx&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;One of the many features in SQL2005 is the ability to create stored procedures, functions (UDFs), and user-defined data types (UDTs)&amp;nbsp;in any .NET language.&amp;nbsp; As might be expected, most of the examples available are in C# and Visual Basic.&amp;nbsp; However, I think that F# should get equal representation, especially since I think that certain features of F# can make it a great language for this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Object expressions to make it easier to re-use "boiler-plate" code for UDTs 
&lt;LI&gt;Easy IEnumerable&amp;lt;&amp;gt; functions for table-valued functions 
&lt;LI&gt;Compositional pickling for simpler binary serialization 
&lt;LI&gt;Integration with LINQ for more sophisticated query processing in UDFs&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;There are still some technical challenges that need to be addressed before F# can be used&amp;nbsp; as a general purpose language with SQL2005 CLR.&amp;nbsp; The biggest problem is that SQL2005 is very strict about the IL it allows to run since it runs "in-process" on the server and the classes and assemblies used can be shared&amp;nbsp;between multiple users.&amp;nbsp; Currently, F# runs afoul of one of the checks:&amp;nbsp; all static fields must be marked as readonly.&amp;nbsp; This restriction&amp;nbsp;is to make sure that one&amp;nbsp;user can't change the contents of a shared class being used by other users.&amp;nbsp; Don Syme is aware of the problem and working on a solution (no release date, yet).&lt;/P&gt;
&lt;P&gt;However, with the release of 1.1.11.7, it is possible to start experimenting with F# and SQL2005 by disabling or bypassing the CLR security checks.&amp;nbsp; By disabling the checks, we can use F#-generated assemblies at the risk of compromising the server.&amp;nbsp; Therefore, I can't recommend this in a production environment&amp;nbsp;unless you have a very clear understanding of the security and stability implications and consequences&amp;nbsp;of the various methods of disabling the checks.&amp;nbsp; See the SQL2005 documentation for more details.&lt;/P&gt;
&lt;P&gt;With these caveats, I'm going to present some sample F# programs&amp;nbsp;which implement SQL2005 CLR objects.&amp;nbsp; These can be run on any SQL2005 installion (including the free Express Edition).&amp;nbsp; They require the use of .NET 2.0 (aka Whidbey) (another free download).&lt;/P&gt;
&lt;P&gt;My plan is to have four more posts over the next week:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Setup and a simple stored procedure 
&lt;LI&gt;User defined functions (both scalar- and table-valued) 
&lt;LI&gt;User defined data types 
&lt;LI&gt;User defined aggregate functions&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;I hope that this alternative look at F#/.NET interoperability will be interesting.&lt;/P&gt;&lt;img src="http://cs.hubfs.net/aggbug.aspx?PostID=377" width="1" height="1"&gt;</content><author><name>LewisBruck</name><uri>http://cs.hubfs.net/members/LewisBruck.aspx</uri></author></entry></feed>