标签: f#

给定一个受成员约束约束的值,我可以作为一等函数访问该成员吗?

是否可以将受约束的成员作为第一类函数(给定对象)进行访问?如果是这样,使用的语法是什么?

  // Example: property getter as a first-class function
  type Test() =
     member x.Value = "42"

  let t = Test()
  let getter = t.get_Value // works as expected


  // now generically:
  let inline getGetter< ^a when ^a : (member get_Value : unit -> string)> item =
    // call getter
    let value = (^a : (member get_Value : unit -> string) item)
    // try to get getter as first-class function
    let getter = item.get_Value // doesn't compile: "Lookup on object of …
Run Code Online (Sandbox Code Playgroud)

f#

0
推荐指数
1
解决办法
136
查看次数

在函数内取消引用ref会产生不同的结果.为什么?

在此示例中,正在热切地评估get_final_answer,并始终返回0.0.我认为包含refs的表达式由于其固有的可变特性而被区别对待(并且在这种情况下并未急切评估).我预计它会返回7.0.

let FinalAnswer = ref 0.0
let get_final_answer = !FinalAnswer

let rec eval_expr_fail =
        FinalAnswer := 7.0
        get_final_answer        // fails, returns 0.0

let rec eval_expr_works =
        FinalAnswer := 7.0
        !FinalAnswer           // works, return 7.0
Run Code Online (Sandbox Code Playgroud)

如何在我更新它的块之外取消引用FinalAnswer?

f#

0
推荐指数
1
解决办法
204
查看次数

F#交互式窗口问题

我在http://www.tryfsharp.org上的交互式窗口中收到此错误.它在视觉工作室工作正常,我不知道如何解决它.任何帮助将不胜感激

let randomNumberGenerator count =
    let rnd = System.Random()
    List.init count (fun numList -> rnd.Next(0, 100))

let rec sortFunction = function
| [] -> []
| l -> let minNum = List.min l in
       let rest = List.filter (fun i -> i <> minNum) l in
       let sortedList = sortFunction rest in
       minNum :: sortedList

let List = randomNumberGenerator 10
let sortList = sortFunction List
printfn "Randomly Generated numbers in a NON-SORTED LIST\n"
printfn "%A" List
printfn "\nSORTED …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive

0
推荐指数
1
解决办法
205
查看次数

在项目euler#5中输入overflow

我正在尝试解决项目euler#5:

2520是可以除以1到10中的每个数字而没有任何余数的最小数字.

可以被1到20的所有数字整除的最小正数是多少?

这是我的代码:

open System

let rec gcd a b =
    match b with
        | x when x = 0 -> a
        | _ -> gcd b (a % b)

let lcm a b = (a * b) / (gcd a b)        
let result = Seq.fold lcm 1 [1..20]

[<EntryPoint>]
let main(args : string[]) =
    printfn "result = %d" result 
    0
Run Code Online (Sandbox Code Playgroud)

它可以正常使用数字[1..19],但是数字[1..20]得到了错误的结果.我试图找出错误的原因,并发现:

$ Seq.fold lcm 1 [1..19]
232792560 // right
$ lcm 232792560 20
18044195 // wrong
Run Code Online (Sandbox Code Playgroud)

看起来类型溢出.我该如何修复错误?

f#

0
推荐指数
1
解决办法
256
查看次数

如何迭代列表并将funciton结果传递给下一次迭代

我是F#的新手并且在努力学习,认为实现聚类算法会很有趣.我有一个列表的输入列表,我需要迭代.对于这些输入向量中的每一个,我需要应用更新权重的函数并返回列表列表(权重矩阵).我可以通过这个newMatrix功能做那个部分.问题是,我需要在下一次迭代中使用更新的权重矩阵,而我却失去了如何做到这一点.这是重要的部分,为简洁起见,遗漏了一些功能.

let inputList = [[1; 1; 0; 0]; [0; 0; 0; 1]; [1; 0; 0; 0]; [0; 0; 1; 1;]]
let weights = [[.2; .6; .5; .9]; [.8; .4; .7; .3]]
let newMatrix xi matrix =
    List.map2( fun w wi ->
        if wi = (yiIndex xi) then (newWeights xi)
        else w) matrix [0..matrix.Length-1]
 printfn "%A" (newMatrix inputList.Head weights)
 > >
 [[0.2; 0.6; 0.5; 0.9]; [0.92; 0.76; 0.28; 0.32]]
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使用之前的结果迭代inputList计算newMatrix每个inputVector newMatrix

编辑:添加了伪造的算法:

for input …
Run Code Online (Sandbox Code Playgroud)

f#

0
推荐指数
1
解决办法
3115
查看次数

为什么这个f#代码中的成员字典总是空的?

我想抓取所有网址的页面并将它们放入字典中.我用字典创建了一个类.但我似乎无法在其中添加元素.

type crawler =

     new()= {}
     member this.urls  = new Dictionary<string,string>()
     member this.start (url : string)=
        let hw = new HtmlWeb()
        let doc = hw.Load(url)
        let docNode = doc.DocumentNode
        let links = docNode.SelectNodes(".//a")

        for aLink in links do
            let href = aLink.GetAttributeValue("href"," ")
            if href.StartsWith("http://")  && href.EndsWith(".html") then
              this.urls.Add(href, href)
Run Code Online (Sandbox Code Playgroud)

为什么字典网址是空的?

f# dictionary

0
推荐指数
1
解决办法
220
查看次数

在VB.NET中重写F#代码

前段时间,我做了一些F#编程,下面的代码用于序列化/反序列化float32数组:#light

但是,现在,我必须在VB.NET中进行一些编程,以再次序列化/反序列化float32数组.由于F#编程不适合GUI,但GUI是我当前编程所必需的.我想知道如何在VB.NET中重写上面的代码.例如,我有一个float32数组(我不知道VB.NET中的数据类型与F#中的float32数组相同)(1.0,2.0,3.0); 我想使用DataContractSerializer序列化它,然后读回来,所以我仍然可以得到相同的float32数组.请分享您的代码.感谢,并有一个愉快的一天!约翰

vb.net f#

0
推荐指数
1
解决办法
202
查看次数

如何定义异常类型和另一种类型之间的递归关系?

创建包含从F#中抛出的类型的异常的最佳方法是什么?以下不起作用:

// this fails since HeaderInfo is not yet defined. Can't seem use the and-keyword
// on HeaderInfo
exception MissingHeader of string*HeaderInfo

type HeaderInfo =
    {
        DefaultHeaderIndices: Map<string, int>;
        AdditionalStudyIndices: Map<string, int>;
        VolumeIndex: int option;
    }
    with member this.GetCommonIndex(name) = 
            match this.DefaultHeaderIndices.TryFind(name) with
            | Some(idx) -> idx
            | None ->
                match this.AdditionalStudyIndices.TryFind(name) with
                | Some(idx) -> idx
                | None ->
                    match this.VolumeIndex with
                    | Some(idx) when name = optionalHeader -> idx
                    | _ -> raise <| MissingHeader(name, this)
Run Code Online (Sandbox Code Playgroud)

谢谢!

f# exception

0
推荐指数
2
解决办法
152
查看次数

使用F#的空值类型

有没有办法得到一个null值的类型?这不起作用:

let a: string = null
let typ = a.GetType()
Run Code Online (Sandbox Code Playgroud)

谢谢

null f# types

0
推荐指数
2
解决办法
217
查看次数

是否有更简单的方法来使用F#可变结构

我现在怎么做真的很奇怪.可悲的是我有很多结构并经常使用它们.

以下是我的表现:

[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type OneDevice = {
        mutable id              : UInt16
        mutable typeDev         : byte
        mutable portNum         : byte
        mutable Parity          : byte
        mutable StopBits        : byte
        mutable BaudRate        : byte
        mutable addr1           : byte 
        mutable addr2           : byte 
        mutable useCanal        : byte
        mutable idGroup1        : byte
        mutable idGroup2        : byte 
        mutable idGroup3        : byte
        mutable idGroup4        : byte
        mutable idGroupSos1     : byte 
        mutable idGroupSos2     : byte 
        mutable idGroupSos3     : byte 
        mutable idGroupSos4     : byte 
        mutable idSosReserv     : …
Run Code Online (Sandbox Code Playgroud)

f# structure

0
推荐指数
1
解决办法
156
查看次数

标签 统计

f# ×10

dictionary ×1

exception ×1

f#-interactive ×1

null ×1

structure ×1

types ×1

vb.net ×1