到现在为止,我有一个类似这样的课程:
type C<'a when 'a :> A> (...)
Run Code Online (Sandbox Code Playgroud)
但现在我创建了一个新的B型:
type B (...) =
inherit A()
Run Code Online (Sandbox Code Playgroud)
但我不希望C支持B,这不编译:
type C<'a when 'a :> A and not 'a :> B> (...)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
正如你所看到的大量问题所见,我真的越来越深入了解F#:)
另一个疑问接近我的学习路径:空值.考虑到由于.NET框架和F#(或框架中的任何其他语言)之间的紧密集成,如何处理它们?
为了简单起见,这里有一段代码:
let myfunc alist =
try
List.find (fun x -> true) alist
with
| :? KeyNotFoundException as ex -> (* should return null *)
Run Code Online (Sandbox Code Playgroud)
如何在函数中返回null?
该null关键字是无用的,除非被识别(不相同nil).
而且,一般来说,处理null返回值时的最佳做法是什么?
嘿伙计们,我正在尝试使用函数式编程(特别是使用F#),并且在构建尾递归函数时我遇到了问题.我很好地将基本递归(函数基本上每次调用一次调用自身)转换为尾递归,但我现在有一个稍微复杂的情况.
在我的例子中,该函数必须接受一个列表作为参数.调用该函数时,我必须从列表中删除第一个元素,然后使用列表的其余部分重复.然后我需要将我以某种方式删除的第一个元素应用于递归的结果.接下来,我删除第二个元素并执行相同的操作(注意:当我说"删除seond元素"时,即来自原始列表,因此在递归时传递的列表也包括第一个元素).我对列表的第三,第四等元素也这样做.
有没有办法将上述情况转换为尾递归函数?也许嵌套的尾递归函数??? 谢谢你的任何答案.
好的,这是我的基本代码.这个特定的一个是置换生成器(我不太关心置换部分,但是 - 这是我想关注的递归):
let permutationsOther str =
match str with
| value :: [] ->
[[value]]
| _ ->
let list = (List.map (fun a -> // This applies the remove part for every element a
let lst = (List.filter (fun b -> b <> a) str) // This part removes element a from the list
let permutedLst = permutations lst // recursive call
consToAll a permutedLst // constToAll this is my own function which performs …Run Code Online (Sandbox Code Playgroud) 这段代码是完整的:
let swap_tuple (a, b) = (b, a)
let result = swap_tuple ("one", "two")
printfn "%A" result
此代码提供编译错误:
let swap_tuple (a, b) = (b, a)
printfn "%A" swap_tuple ("one", "two")
error FS0001: Type mismatch. Expecting a 'a -> 'b -> 'c but given a 'a -> unit The type ''a -> 'b' does not match the type 'unit'
第二个版本有什么问题?
与F#进行模式匹配时遇到问题.我正在构建一个F#库,到目前为止这个:
namespace parser
module parse =
let public y = function
| x when x.Contains("hi") -> "HELLO"
| x when x.Contains("hello") -> "HI"
| x -> x
Run Code Online (Sandbox Code Playgroud)
但它给了我错误:根据此程序点之前的信息查找不确定类型的对象的错误.在此程序点之前可能需要类型注释来约束对象的类型.这可以允许解析查找.
我想定义一个int和float的列表,其中[1,2.0]应该是一个有效的构造.
例如,F#在类型或类型层次结构中是否具有int和从数字派生的浮点数?
谢谢.
let m = Regex.Match(X.Text, "\\b(select)|(where)|(from)\\b", RegexOptions.IgnoreCase)
Run Code Online (Sandbox Code Playgroud)
它只突出了Select,所以我猜麻烦在于我的Regex.Match语法,但我看不到哪里?
与alll相关的变化我当前的解决方案看起来像这样:
module SQL_Highlighing
open System.Runtime.InteropServices
module Lock =
[<DllImport(@"User32", CharSet = CharSet.Ansi, SetLastError = false, ExactSpelling = true)>]
extern void LockWindowUpdate(int hWnd)
open System.Text.RegularExpressions
open System.Drawing
type SyntaxRTB() =
inherit System.Windows.Forms.RichTextBox()
override X.OnTextChanged(e : System.EventArgs) =
base.OnTextChanged(e); X.ColorTheKeyWords()
member X.ColorTheKeyWords() =
let HL s c =
let color(m : Match, color : Color) =
X.SelectionStart <- m.Index
X.SelectionLength <- m.Length
X.SelectionColor <- color
Regex.Matches(X.Text, "\\b" + s + "\\b", RegexOptions.IgnoreCase) |> fun mx …Run Code Online (Sandbox Code Playgroud) 我有一个F#异常,它没有被正确的catch块捕获.
这是相关的代码:
exception ConfigFileVersionIncompatabilityException of string
[<XmlType("config")>]
type Configuration() = class
let thisVersion : string = "1.0"
let mutable fileVersion : string = thisVersion
[<XmlAttribute("version")>]
member x.FileVersion
with get() = fileVersion
and set v = if v <> thisVersion
then raise (ConfigFileVersionIncompatabilityException(String.Format("Was expecting version {0} but read version {1}.", thisVersion, v)))
end
module FilterFileFunctions =
let sampleConfigFilename = "sample.filters"
let readConfig (file : string) =
try
use xmlDoc = new StreamReader(file) in
let s = XmlSerializer(typeof<Configuration>)
s.Deserialize(xmlDoc) :?> Configuration |> …Run Code Online (Sandbox Code Playgroud) 我有一个seq<'A>.我想将它映射到a seq<(int, 'A)>,其中整数是从0开始的自动生成的值序列.我知道我可以用可变计数器和循环来做这个,但是有更优雅的方法来做到这一点,也许使用Seq.map?
我正在为F#开发一个实体框架代码第一包装器,我一直在想我是否应该将所有模块合并为一个.
看看这个:
module ManyNavProperty =
let withMany (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithMany()
let withSeq expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithSeq expr
let withList expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithList expr
let withArray expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithArray expr
let withOptional (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional()
let withOptionalProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional expr
let withRequired (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequired()
let withRequiredProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequiredProperty expr
module DependentNavProperty =
let hasForeignKey expr (cfg:DependentNavPropertyInfo<'a>) = cfg.HasForeignKey expr
module CascadableNavProperty =
let willCascadeOnDelete b (cfg:CascadableNavPropertyInfo) = cfg.WillCascadeOnDelete b
module EF =
let entity<'a …Run Code Online (Sandbox Code Playgroud)