为什么没有List.skip和List.take?当然有Seq.take和Seq.skip,但它们不会创建列表.
一种可能的解决方案是:mylist |> Seq.skip N |> Seq.toList但是这会创建第一个枚举器,然后从该枚举器创建一个新列表.我认为可以有更直接的方法从不可变列表创建不可变列表.由于内部没有复制元素,因此只有从新列表到原始列表的引用.
其他可能的解决方案(不抛出异常)是:
let rec listSkip n xs =
match (n, xs) with
| 0, _ -> xs
| _, [] -> []
| n, _::xs -> listSkip (n-1) xs
Run Code Online (Sandbox Code Playgroud)
但这仍然没有回答这个问题......
为什么这么简单的代码不起作用?
import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
main :: IO ()
main = simpleHttp "http://www.dir.bg/" >>= L.putStr
Run Code Online (Sandbox Code Playgroud)
它会导致以下错误:
TestConduit.exe:InternalIOException getAddrInfo:不存在(错误10093)
关于F#中元素组合的最优雅和简单实现的另一个问题.
它应该返回输入元素的所有组合(列表或序列).第一个参数是组合中的元素数量.
例如:
comb 2 [1;2;2;3];;
[[1;2]; [1;2]; [1;3]; [2;2]; [2;3]; [2;3]]
Run Code Online (Sandbox Code Playgroud) 在什么情况下,F#中的列表通过F#编译器优化到数组,for循环,while循环等,而不创建单个链接数据的实际列表?
例如:
[1..1000] |> List.map something
Run Code Online (Sandbox Code Playgroud)
可以针对for循环进行优化,而无需创建实际列表.但我不知道编译器是否真的这样做了.
可以使用循环展开等优化在大小较小的列表上进行映射.
根据https://docs.oracle.com/cd/E36784_01/html/E36873/librt-3lib.html
从历史上看,此库中的函数提供了POSIX.1b实时扩展指定的许多接口.见标准(5).此功能现在位于libc(3LIB)中.
维护此库以提供运行时和编译环境的向后兼容性.共享对象在libc.so.1上实现为过滤器.新的应用程序开发不需要指定-lrt.
所以目前它应该只链接到libc,libc应包含所有内容.同样适用于libc中包含的libdl依赖.
我这样做:
import qualified Data.ByteString.Lazy.Char8 as BS
main :: IO ()
main = do
let filename = "sample.txt"
text <- BS.readFile filename
let res = BS.take 1000 $ text
print res
Run Code Online (Sandbox Code Playgroud)
当我通过分析运行它时,它给了我:
162,048 bytes allocated in the heap
2,472 bytes copied during GC
59,688 bytes maximum residency (1 sample(s))
22,232 bytes maximum slop
156 MB total memory in use (0 MB lost due to fragmentation)
Run Code Online (Sandbox Code Playgroud)
我读的文件大约是50K字节.为什么需要60K字节的内存(最大驻留时间)?我也尝试过String和Lazy文本.这是相同的图片.我认为Haskell在某种程度上是将整个文件读入内存或只是分配与文件长一样多的内存.我怎么能阻止这个?我想从它只读N字节,不想浪费这么多的内存.
有人可以提出更好和/或更优雅的实现:
let each xs =
let rec each' acc left right =
match right with
| [] -> acc
| right -> let new_left = left @ [List.hd right]
let next = List.tl right
let result = (List.hd right), left @ next
each' (result::acc) new_left next
each' [] [] xs
它做到了:
> each [1..3];; val it : (int * int list) list = [(3, [1; 2]); (2, [1; 3]); (1, [2; 3])]
此函数也可以反向返回结果.我们的想法是将所有元素作为元组,包含元素和rest元素列表.
对于该类型:
Record Version := mkVersion {
major : nat;
minor : nat;
branch : {b:nat| b > 0 /\ b <= 9};
hotfix : {h:nat| h > 0 /\ h < 8}
}.
Run Code Online (Sandbox Code Playgroud)
我正在尝试举一个例子:
Example ex1 := mkVersion 3 2 (exist _ 5) (exist _ 5).
Run Code Online (Sandbox Code Playgroud)
它失败了:
术语“存在?P 5”的类型为“?P 5-> {x:nat |?P x}”,而术语为“ {b:nat | b> 0 / \ b <= 9}”。 。
我想念什么?
我正在尝试序列化"vector"(Microsoft.FSharp.Math)类型.我收到了这个错误:
异常详细信息:System.Runtime.Serialization.SerializationException:键入数据协定名称为"Instances.FloatNumerics_x0040_115:http://schemas.datacontract.org/2004/07/Microsoft "的"Microsoft.FSharp.Math.Instances+FloatNumerics@115" . FSharp.Math '不是预期的.将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中.
我试图把KnownType属性和其他一些东西,但没有任何帮助!
有人能知道答案吗?这是我使用的代码:
// [< KnownType( typeof<vector> ) >]
type MyType = vector
let public writeTest =
let aaa = vector [1.1;2.2]
let serializer = new DataContractJsonSerializer( typeof<MyType> )
let writer = new StreamWriter( @"c:\test.txt" )
serializer.WriteObject(writer.BaseStream, aaa)
writer.Close()
Run Code Online (Sandbox Code Playgroud) 如何进一步优化此代码:
let rec nCollect func = function
| [] -> []
| x::xs -> let firstAndNext body (firstBody, ys) =
let newFirst, z = func firstBody body
newFirst, z::ys
let y, ys = List.foldBack firstAndNext xs (x, [])
y::(nCollect func ys)
Run Code Online (Sandbox Code Playgroud)
此代码是nbody仿真程序的一部分.它正在获取每个正文并在它和下一个之间应用函数函数.结果用于下一次迭代.我用列表略微优化了它.问题是输入体的计数低于10,但nCollect被称为数百万次.例如,如果我在nCollect中使用尾递归,结果会更糟.
我想从Either monad转换为IO,而没有Either的任何线索。有更优雅的方法吗?
我已经编写了此函数,但是我想使用库或Prelude中的一些东西:
liftEither :: forall t (m :: * -> *) a. Monad m => (t -> m a) -> Either String t -> m a
liftEither f (Right a) = f a
liftEither _ (Left msg) = fail msg
Run Code Online (Sandbox Code Playgroud) 两种方法MyBehavior :: CreateSerializer()都没有被调用,但是ReplaceBehavior()方法正在工作.它正在使用我的自定义更改默认行为.
有人知道问题出在哪里?
关键是编写自定义REST WCF序列化程序,该序列化程序应生成非XML文本格式结果.
public class MySerializerFormatAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
{
}
public void Validate(OperationDescription description)
{
}
private static void ReplaceBehavior(OperationDescription description)
{
DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcsOperationBehavior != null)
{
int idx = description.Behaviors.IndexOf(dcsOperationBehavior);
description.Behaviors.Remove(dcsOperationBehavior);
description.Behaviors.Insert(idx, new MyBehavior(description));
}
}
public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
{
ReplaceBehavior(description);
}
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
ReplaceBehavior(description);
}
};
public class MySerializer : XmlObjectSerializer
{
public override bool …Run Code Online (Sandbox Code Playgroud)