From Don Syme's blog,we know Visual Studio Express 2012 for Web is a free development environment that programmers can use to build projects such as ASP.NET applications and Windows Azure cloud services. This F# Tools release adds in F# 3.0 components, such as the F# 3.0 compiler, F# Interactive, IDE support, and new F# features such as type providers and query expressions.
In my mind, if we use Visual Studio 2012 Professional or higher version to create F# project, we may not use F# Tutorial template. But if we use Visual Studio Express 2012 for Web, when we create F# Tutorial template, it is another way to create a F# Console Application.
Sunday, November 4, 2012
Friday, November 2, 2012
An Classic and Interesting issue in Math
In China, there are some interesting issues in math. Now let us enjoy one of them, a very classic indeterminate equation question--in ancient China, how to use one hundred penny(penny is the unit of measurement of money in ancient China) to buy one hundred chicken.
The topic is very simple: one rooster will cost 5 penny, one hen will cost 3 penny, but three chicken only cost 1 penny. Now, you have 100 penny to buy them, the requirements are as follows:
1. all kinds of chicken you should buy.
2. how many roosters, hens and chicks you buy will cost 100 penny?
Analysis: we can set up a rooster is x, the hen is y Chicken is z, then we can draw the indeterminate equation:
x+y+z=100
5x+3y+z/3=100
As we have only 100 penny, we can get the result 0<5x<100, so 0<x<20, similarly, we will get result 0<y<33 and z=100-x-y.
Until now, the program comes out:
let rooster()=
for x=1 to 19 do
for y=1 to 32 do
let z= 100-x-y
if ((z%3=0)&&(5*x+3*y+z/3=100)) then
printfn "You should buy %d rooster, %d hen, %d chicks" x y z
It is a very easy quetion, is it?
The topic is very simple: one rooster will cost 5 penny, one hen will cost 3 penny, but three chicken only cost 1 penny. Now, you have 100 penny to buy them, the requirements are as follows:
1. all kinds of chicken you should buy.
2. how many roosters, hens and chicks you buy will cost 100 penny?
Analysis: we can set up a rooster is x, the hen is y Chicken is z, then we can draw the indeterminate equation:
x+y+z=100
5x+3y+z/3=100
As we have only 100 penny, we can get the result 0<5x<100, so 0<x<20, similarly, we will get result 0<y<33 and z=100-x-y.
Until now, the program comes out:
let rooster()=
for x=1 to 19 do
for y=1 to 32 do
let z= 100-x-y
if ((z%3=0)&&(5*x+3*y+z/3=100)) then
printfn "You should buy %d rooster, %d hen, %d chicks" x y z
It is a very easy quetion, is it?
Thursday, November 1, 2012
Why continuation function also throw overflow exception
Now we know, we can use continuation function to avoid the overflow exception, but first time when we use continuation, many of us may confuse to encounter the overflow exception again if you run your continuation program in Visual Studio editor with F5 or Ctrl+F5, what's worse, the overflow exception will appear even the data is really small. But if you try some other way to run the same continuation function, like FSI or FSC, you will encounter a very interesting result. It works well in FSI and FSC, but failed only in Visual Studio editor. Here we use Fibonacci continuation as the example, please see the failed screenshot below:
Is it a bug in Visual Studio? It is the first reaction in my mind, but after talking with Tao Liu, he told me the answer is No! Don't worry, it is because we forget that tail recursion is turned off by default when using Visual Studio and compiling in Debug mode, and how should we do now? Yeah, we can turn on it in project property:
There is another way to workaround this exception, that is switch your program from 'Debug' mode to 'Release' mode. OK, now, just try to run your program again, you will see the real result you want.
Note: you can still find the answer here.
Is it a bug in Visual Studio? It is the first reaction in my mind, but after talking with Tao Liu, he told me the answer is No! Don't worry, it is because we forget that tail recursion is turned off by default when using Visual Studio and compiling in Debug mode, and how should we do now? Yeah, we can turn on it in project property:
- right-click the project properties in Solution Explorer, select 'Properties'
- go to the 'Build' tab
- make sure 'Debug' configuration is selected
- click the box 'Generate tail calls'
There is another way to workaround this exception, that is switch your program from 'Debug' mode to 'Release' mode. OK, now, just try to run your program again, you will see the real result you want.
Note: you can still find the answer here.
Different efficiency between Recursion and Continuation
As we all know, we often use recursive function in our program, and we often encounter the stack overflow exception. In blog F# Continuation Style Programming , we learn how to change the recursive into continuation to avoid this exception. Of course, if the data is small, we will think which one is better to use.
After testing the running time between Recursive and Continuation function with small data, here we use the example of Fibonacci which is known to us, then we can get the results as follows:
As the result is small to computer, there is no obvious difference performance in CPU usage and performance between Recursive and Continuation, so we measure the running time to find the result.
Below is the running time for Recursive function:
Below is the running time for Continuation function:
Now, we can see, if the data is small, the Recursive will cost less time than Continuation. So when the data is small, recursive function is the better choice we should choose.
After testing the running time between Recursive and Continuation function with small data, here we use the example of Fibonacci which is known to us, then we can get the results as follows:
As the result is small to computer, there is no obvious difference performance in CPU usage and performance between Recursive and Continuation, so we measure the running time to find the result.
Below is the running time for Recursive function:
Below is the running time for Continuation function:
Subscribe to:
Posts (Atom)