f#

Haskell syntax for 'or' in case expressions

孤者浪人 提交于 2019-12-18 18:49:41
问题 In F#, I can use | to group cases when pattern matching. For example, let rec factorial n = match n with | 0 | 1 -> 1 // like in this line | _ -> n * factorial (n - 1) What's the Haskell syntax for the same? 回答1: There is no way of sharing the same right hand side for different patterns. However, you can usually get around this by using guards instead of patterns, for example with elem. foo x | x `elem` [A, C, G] = ... | x `elem` [B, D, E] = ... | otherwise = ... 回答2: with guards: factorial n

F# printf string

耗尽温柔 提交于 2019-12-18 18:35:58
问题 Im puzzled let test = "aString" let callMe = printfn test Why isn't this working? Throws below error at compile time: The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>' This works fine: printfn "aString" 回答1: That's because the format parameter is not actually a string . It's TextWriterFormat<'T> and the F# compiler converts the string format into that type. But it doesn't work on string variables, because the compiler can't convert the string to TextWriterFormat<

F# printf string

喜夏-厌秋 提交于 2019-12-18 18:35:50
问题 Im puzzled let test = "aString" let callMe = printfn test Why isn't this working? Throws below error at compile time: The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>' This works fine: printfn "aString" 回答1: That's because the format parameter is not actually a string . It's TextWriterFormat<'T> and the F# compiler converts the string format into that type. But it doesn't work on string variables, because the compiler can't convert the string to TextWriterFormat<

Meaningful errors during parsing with FSyacc

佐手、 提交于 2019-12-18 17:36:29
问题 I'm using fsyacc/fslex from F# Power Pack to parse some source code. To detect errors I use the following code: use inputChannel = new StreamReader(File.OpenRead tempFileName) let lexbuf = Lexing.LexBuffer<_>.FromTextReader inputChannel let ast = try Parser.start Lexer.tokenize lexbuf with e -> let pos = lexbuf.EndPos let line = pos.Line let column = pos.Column let message = e.Message let lastToken = new System.String(lexbuf.Lexeme) printf "Parse failed at line %d, column %d:\n" line column

Lazy cartesian product of multiple sequences (sequence of sequences)

人走茶凉 提交于 2019-12-18 15:53:02
问题 Can you suggest simpler and clearer way to write this function? let cartesian_product sequences = let step acc sequence = seq { for x in acc do for y in sequence do yield Seq.append x [y] } Seq.fold step (Seq.singleton Seq.empty) sequences 回答1: Less elegant, but (seems to be) faster solution: let cartesian_product2 sequences = let step acc sequence = seq { for x in acc do for y in sequence do yield seq { yield! x ; yield y } } Seq.fold step (Seq.singleton Seq.empty) sequences ; > cartesian

State Monad, why not a tuple?

青春壹個敷衍的年華 提交于 2019-12-18 15:35:55
问题 I've just wrapped my head around monads (at least I'd like to think I have) and more specifically the state monad, which some people that are way smarter then me figured out, so I'm probably way of with this question. Anyway, the state monad is usually implemented with a M<'a> as something like this (F#): type State<'a, 'state> = State of ('state -> 'a * 'state) Now my question: Is there any reason why you couldn't use a tuple here? Other then the possible ambiguity between MonadA<'a, 'b> and

How to generate the F# type signature similar to FSI in my own code?

主宰稳场 提交于 2019-12-18 15:09:30
问题 If one uses the F# Interactive Shell (FSI), the inferred expression type (signature) is printed to the console along with its value: val it : int * string * float = (42, "Hello F#", 42.0) How can I mimick the same behaviour in my own code, e.g. to get the inferred types as string for a F# expression? I don't need to dynamically evaluate any F# expressions, the expressions are known in compile time and are part of my (static) F# code. I need this feature to be able to mimick the FSI output in

F# Seq.sortBy in descending order

孤街醉人 提交于 2019-12-18 14:03:44
问题 I am fairly new to F# and came by the Seq.sortBy function however it is sorting my list in ascending order. How do I get it to sort in descending order using the Seq.sort? For instance an example code would be... let DisplayList = seq{0..10} |> Seq.sortBy(fun x -> x) |> Seq.iter(fun x -> Console.WriteLine(x.ToString())) gives me an output of 1 2 3 4 5 6 7 8 9 10, when I really want it to do it from 10 to 1. 回答1: Looking at the other answers, beware unary minus and MININT: let a = [| 1; -1;

F# programmatically running .fsx script file

大憨熊 提交于 2019-12-18 13:39:15
问题 I'm sure this must be something really easy, but I can't seem to make it work. Let's say I have an .fsx script file and want to cause it to be executed programmatically. I'm guessing someone must have blogged about this at some point, but I can't seem to find an example that performs my simple scenario. Basically, I want to programmatically duplicate what happens when you right click on an .fsx file and choose "Run with F# Interactive..." 回答1: As asked in a comment, you can set

fsx script referencing a dll referencing many dll

爷,独闯天下 提交于 2019-12-18 13:10:03
问题 What kind of strategy do I have for the following problem. I want to use a simple class inside a dll, which has link to various dlls, of various versions etc. As a fsx file, my script show no error. but upon running it in fsharp interactive, it tells me error FS0074: The type referenced through 'theTypeIWantToUse' is defined in an assembly that is not referenced. You must add a reference to assembly 'Assembly'. Of course the assembly is referenced, so I imagine I need to add references to