I'm trying to rewrite this matlab .Net example in F# interactive.
In the example a COM-interface reference is used.
How do I reference a COM-interface from within the fsi?
EDIT
I refence the as described
From the Project menu, select Add Reference. Select the COM tab in the Add Reference dialog box. Select the MATLAB application.
This reference I can not send with right click to the interactive. There is no path in the properties and the dll cited in the description MLApp.dll I can not find in my matlab directory. This F# programm works:
[<EntryPoint>]
let main argv =
let matlab = new MLApp.MLAppClass()
matlab.Execute "x=1+1" |> printfn "%A"
matlab.GetVariable("x","base") |> printfn "%A"
0
But I want to use it from FSI.
Related
I want to write a quantum program in F# but I don't know how to call Q# operations from F#. How exactly would I do this?
I've tried reading the C# version first but it doesn't seem to translate well to F#.
TL;DR: You have to create a Q# library project (which will yield a .csproj project with only Q# files in it) and to reference it from a purely F# application.
You can not mix F# and Q# in the same project, because it won't compile: Q# compiles to C# for local simulation, and you can't have C# and F# in the same projects. However, you can have two separate projects in different languages which both compile to MSIL and can reference each other.
The steps are:
Create Q# library QuantumCode and write your code in it.
Let's say your code has an entry point with the signature operation RunAlgorithm (bits : Int[]) : Int[] (i.e., it takes an array of integers as a parameter and returns another array of integers).
Create an F# application (for simplicity let's make it a console app targeting .NET Core) FsharpDriver.
Add a reference to the Q# library to the F# application.
Install the NuGet package Microsoft.Quantum.Development.Kit which adds Q# support to the F# application.
You will not be writing any Q# code in FsharpDriver, but you will need to use functionality provided by the QDK to create a quantum simulator to run your quantum code on, and to define data types used to pass the parameters to your quantum program.
Write the driver in F#.
// Namespace in which quantum simulator resides
open Microsoft.Quantum.Simulation.Simulators
// Namespace in which QArray resides
open Microsoft.Quantum.Simulation.Core
[<EntryPoint>]
let main argv =
printfn "Hello Classical World!"
// Create a full-state simulator
use simulator = new QuantumSimulator()
// Construct the parameter
// QArray is a data type for fixed-length arrays
let bits = new QArray<int64>([| 0L; 1L; 1L |])
// Run the quantum algorithm
let ret = QuantumCode.RunAlgorithm.Run(simulator, bits).Result
// Process the results
printfn "%A" ret
0 // return an integer exit code
I posted a full example of the project code here (originally that project dealt with using Q# from VB.NET, but for F# all the steps are the same).
I have written a F# script in FSI using Ionide in VS Code. It's a great tool, but I am getting a warning from Ionide Lint suggesting a code improvement:
'Lint: Seq.map f (Seq.map g x) might be able to be refactored into Seq.map (g >> f) x.'
I have about 6 Seq.map functions all piped together with |> which I am happy with.
There is also a green wiggly line that is annoying me. I don't agree with the suggestion, and want the wiggly line to go away. How can I tell Ionide to stop making this suggestion?
I have turned off Lint globally in the VS Code settings
"FSharp.linter": false,
I think Ionide uses FsharpLint: http://fsprojects.github.io/FSharpLint/
This supports suppressing of lint messages like this:
[<SuppressMessage("NameConventions", "InterfaceNamesMustBeginWithI")>]
type Printable =
abstract member Print : unit -> unit
Something like that might work for you as well. I just turned it off.
This is the directive to disable this particular message in code:
open System.Diagnostics.CodeAnalysis
[<SuppressMessage("Hints", "") >]
Place above the block that is producing this 'error'.
I try to play with fsharp under Ubuntu (and yes, I slowly figure out that it is more pain than fun), I already installed Mono, VSCode and Ionide extension and I can create and build F# projects. Unfortunately when I run simple F# script via F# Interactive:
printfn "bar"
In terminal window I get:
>
- printfn "bar"
-
- ;;
bar
val it : unit = () F# 4.0 (Open Source Edition)
> ^?^?414;3R^?^?^?^?^?^? the Apache 2.0 Open Source License
The strange sequence ^?^? looks like unrecognized terminal escape codes, but when I use bash from within VSCode there is nothing like this.
What's more the strange sequence reappears after every command executed in FSI:
> let j = 9;;
val j : int = 9
> printfn "foo";;
foo
val it : unit = ()
> ^?^?
Does anyone have the same problem and knows a solution (or maybe just knows a solution)?
EDIT: Problem occurs mostly when I execute commands via Ionide Alt+Enter shortcut
This looks like the https://github.com/Microsoft/vscode/issues/19766 bug. VS Code 1.9 introduced a new setting, terminal.integrated.flowControl, that defaults to true. The ^? characters you're seeing (and any ^S and ^Q characters that might show up) come from this "flow control" feature, which doesn't play well with F# Interactive. Change your VS Code settings to set terminal.integrated.flowControl to false and your problem should go away.
I come from C# background to F#. So far I wrote simple programs and spent a lot of time in F# interactive.
I'm stuck creating a VS F# project with two .fs files.
Sample code:
// part 1: functions
let rec gcd (a : uint64) (b : uint64) =
if b = 0UL then a
else gcd b (a % b)
// part 2: main()
let a, b = (13UL, 15UL)
do printfn "gcd of %d %d = %d" a b (gcd a b)
I'd like to have two .fs files, namely, Alg.fs and Program.fs, so that Program.fs would contain the code I'm working and Alg.fs having algorithms.
Taken steps:
I've created the two files. Compiler gave an error: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
I've inserted module Program and module Alg. The complied program executes only the code from Alg.fs completely ignoring Program.fs...
I'm using F# 2.0 in Visual Studio 2010.
P.S. I've googled and checked some posts, and read documentation on modules and saw relative questions before asking.
Sounds like this is an order-of-files-in-the-project issue. The last file is the entry point ("main method"), sounds like you have Alg.fs last, and you need Program.fs last. You can re-order them via the right-click context menu in VS Solution Explorer.
There are at least three separate things that need to be looked at here:
As mentioned by #Brian, the order of source control files is also the compile order. This matters in F# where type inference is heavily used. Make sure Alg.fs comes before Program.fs in your Visual Studio file list (try this: select Program.fs and hit Alt+Down Arrow until it's at the bottom).
Since Alg.fs and Program.fs are now in modules, you need to actually open the Alg module in Program to get access to its bindings (open Alg), or add the [<AutoOpen>] attribute on Alg.
As #Daniel says, the last problem could be the definition of the entry point to the program. You need either an [<EntryPoint>] attribute on a top level binding that is also the last function in the last file. Alternatively, this defaults to the last binding in the last file anyway, just make sure it has the right signature (see Daniel's link).
How do you use Console.Readline in F#? Unlike Console.Writeline, it isn't being honored when I call it.
If you use
let s = Console.ReadLine
you are only building a delegate that points to the ReadLine function. You need to say
let s = Console.ReadLine()
to actually execute the function. This is just like C# syntax, except type inference means you don't get a compiler warning.
What do you mean by "it isn't being honored"? Here's a small console app I've just written in VS2010b1, and it works fine:
open System
let line = Console.ReadLine()
Console.WriteLine("You wrote {0}", line)
// Just to make it pause
let unused = Console.ReadLine()
Are you trying to run the code from F# Interactive within Visual Studio? If so, that may be the issue, as Brian's post explains.
However, I haven't seen the same problem when using F# Interactive from the command line. Here's a complete transcript of a session:
Microsoft F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.6.16, compiling for .NET Framework Version v4.0.20506
Please send bug reports to fsbugs#microsoft.com
For help type #help;;
> open System;;
> let line = Console.ReadLine();;
Hello world
val line : string = "Hello world"
Running Brian's looping code from F# Interactive didn't show the same problem.
Bottom line: It seems like this is broken in F# Interactive in Visual Studio, but not when running interactively from the command line or in a full console app.
I don't have a Beta1 box handy, but I know that in the past we've had a bug where ReadLine() would see the background commands that communicate between the interactive UI and the background process that runs your F# code. It may be interesting to investigate what
let Foo max =
let rec Loop i =
if i < max then
let line = System.Console.ReadLine()
printfn "line = %s" line
Loop (i+1)
Loop 1
Foo 12
prints when you highlight it and 'Send to Interactive'. I think possibly you'll see a few unexpected interesting lines, followed by lines you type into the window.
// is the right way if you're not wanting to use a return of anything typed into readline
Console.ReadLine() |> ignore