在尝试复杂的例子之前,我试图弄清楚F#的基础知识.我正在学习的材料引入了Discriminate Unions和Record类型.我已经审查了两者的材料,但我仍然不清楚为什么我们会使用一个而不是另一个.
我创建的大多数玩具示例似乎都可以在两者中实现.记录似乎与我认为的C#中的对象非常接近,但我试图避免依赖映射到c#作为理解F#的方法
所以...
是否有明确的理由使用一个而不是另一个?
是否存在适用的某些规范案例?
是否有某些功能可用于一个,而不是另一个?
我有以下功能,它做我想要的.但是使用concat(@)运算符,它是O(n)而不是O(1)运算符
let myFunc s m cs =
let n = s * m
let c = [n - s] // single element list
(n, cs @ c) // concat the new value to the accumulated list
let chgLstAndLast =
[0.99; 0.98; 1.02]
|> List.fold (fun (s, cs) m -> myFunc s m cs) (1., [])
Run Code Online (Sandbox Code Playgroud)
chgLstAndLast返回最后生成的结果值和列表:
val chgLstAndLast : float * float list = (0.989604, [-0.01; -0.0198; 0.019404])
Run Code Online (Sandbox Code Playgroud)
我想以三种方式改进上述内容.
例如,我想写一个myFunc这样的
let myFunc s m cs …Run Code Online (Sandbox Code Playgroud) 我想利用Vector<'T>这里提到的数据结构作为答案
但我似乎无法正确使用语法.例如:
open FSharpx.Collections
let tuple1= (1,2.0,"f")
let tuple2= (2,3.0,"f")
let myLstOfTuples = [tuple1;tuple2]
let myVector = ?? <- how do I do this?
Run Code Online (Sandbox Code Playgroud)
如何创建类型向量Vector<int * float * string>并使用数据填充它?
我有以下要在F#中使用的C#类
using System;
using System.Collections.Generic;
using System.Text;
namespace DataWrangler.Structures
{
public enum Type { Trade = 0, Ask = 1, Bid = 2 }
public class TickData
{
public string Security = String.Empty;
public uint SecurityID = 0;
public object SecurityObj = null;
public DateTime TimeStamp = DateTime.MinValue;
public Type Type;
public double Price = 0;
public uint Size = 0;
public Dictionary<string, string> Codes;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在F#中创建它的一个实例.我用来执行此操作的代码位于f#脚本文件中
#r @"C:\Users\Chris\Documents\Visual Studio 2012\Projects\WranglerDataStructures\bin\Debug\WranglerDataStructures.dll"
open System
open System.Collections.Generic;
open System.Text;
open DataWrangler.Structures …Run Code Online (Sandbox Code Playgroud)