我想将这个ML代码翻译成F#.
fun take ([], i) = []
| take (x::xs, i) = if i > 0 then x::take(xs, i-1)
else [];
Run Code Online (Sandbox Code Playgroud)
我试过这个
let rec take n i =
match n,i with
| [], i -> []
| x::xs, i -> if i > 0 then x::take(xs, i-1)
else [];
let val = take [1;2;3;4] 3
Run Code Online (Sandbox Code Playgroud)
还有这个
let rec take input =
match input with
| ([], i) -> []
| (x::xs, i) -> if i > 0 then x::take(xs, i-1) …Run Code Online (Sandbox Code Playgroud) 我有一个Clojure程序,它返回even下面的一个懒惰的斐波纳契数列的总和n:
(defn sum-of-even-fibonaccis-below-1 [n]
(defn fib [a b] (lazy-seq (cons a (fib b (+ b a)))))
(reduce + (take-while (partial >= n) (take-nth 3 (fib 0 1)))))
(time (dotimes [n 1000] (sum-of-even-fibonaccis-below-1 4000000))) ;; => "Elapsed time: 98.764msecs"
Run Code Online (Sandbox Code Playgroud)
效率不高.但是,如果我不减少序列并简单地返回值列表,(0 2 8 34 144...)它可以更快地完成其工作20倍:
(defn sum-of-even-fibonaccis-below-2 [n]
(defn fib [a b] (lazy-seq (cons a (fib b (+ b a)))))
(take-while (partial >= n) (take-nth 3 (fib 0 1))))
(time (dotimes [n 1000] (sum-of-even-fibonaccis-below-2 4000000))) ;; …Run Code Online (Sandbox Code Playgroud) 已经在同一个网站上阅读了一篇关于这个帖子但没有用的帖子,我感觉有点难过,但我确信我之前已经这样做了.
我有一本词典.我想从Dictionary中获取前200个值.
Dictionary<int,SomeObject> oldDict = new Dictionary<int,SomeObject>();
//oldDict gets populated somewhere else.
Dictionary<int,SomeObject> newDict = new Dictionary<int,SomeObject>();
newDict = oldDict.Take(200).ToDictionary();
Run Code Online (Sandbox Code Playgroud)
显然,take返回一个IENumerable,所以你必须运行ToDictionary()将它转换回相同类型的字典.但是,它只是不起作用,它想要一些随机的键选择器 - 或者什么?我甚至尝试过施展,但无济于事.有任何想法吗?
如何获取数据表的最后5行?我试过这样的事情:
var Long_bottom = LongSlection.Last(5);
Run Code Online (Sandbox Code Playgroud)
其中LongSlection是DataRow.但我有一个错误,任何想法?
我正在查看一些LINQ示例,因此提醒他们应该在最后有一个"select"子句.
但我有一个LINQ工作,没有"选择":
public IEnumerable<InventoryItem> Get(string ID, int packSize, int CountToFetch)
{
return inventoryItems
.Where(i => (i.Id.CompareTo(ID) == 0 && i.PackSize > packSize) || i.Id.CompareTo(ID) > 0)
.OrderBy(i => i.Id)
.ThenBy(i => i.PackSize)
.Take(CountToFetch)
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
这是因为:
(a) select is not really necessary?
(b) Take() is doing the "select"
(c) ToList() is doing the "select"
Run Code Online (Sandbox Code Playgroud)
说实话,这是在我添加"ToList()"之前的工作......所以看起来LINQ在允许一个人逃脱的方面是非常宽容/宽松的.
另外,在我正在使用的LINQ中,我认为OrderBy和ThenBy是冗余的,因为用于填充inventoryItems的SQL查询已经具有ORDER BY ID,PackSize子句.我是对的(.OrderBy()和.ThenBy()是不必要的)?
所以我遇到了这个函数定义,它接受一个列表(xs)并将其分成三部分作为输出.
问题是我很难理解它.我理解第一部分采用列表中的n(xs),其中n被定义为将列表的长度(xs)除以3.但在那之后,我不确定是否已经确定了什么.
如果有人能指导我完成这项功能,那就太棒了.
这是代码:
--SPLITS A LIST INTO THREE PARTS---------------------------------------------------------
split3 xs = (take n xs , take n (drop n xs) , drop (n*2) xs)
where n = length xs `div` 3
Run Code Online (Sandbox Code Playgroud) 嗨,我是哈斯克尔的初学者.我想从这个元组列表中获取前3个项目:
[("and",2),("cat",1),("dog",1),("rabbit",1),("the",2)]
Run Code Online (Sandbox Code Playgroud)
首先,我按频率和降序对列表进行排序:
sortWords = sortBy(flip compare `on` snd)
Run Code Online (Sandbox Code Playgroud)
这给了我结果:
[("and",2),("the",2),("cat",1),("dog",1),("rabbit",1)]
Run Code Online (Sandbox Code Playgroud)
然后我知道我可以做这个功能:
take 3 [("and",2),("the",2),("cat",1),("dog",1),("rabbit",1)]
Run Code Online (Sandbox Code Playgroud)
这给了我想要的结果[("and",2),("the",2),("cat",1)]
但是,我希望能够将该take功能合并到该sortWords功能中.问题是,当我尝试这样做时,例如:
sortWords = take 3 (sortBy(flip compare `on` snd))
Run Code Online (Sandbox Code Playgroud)
这不起作用.
理想情况下,我想保留sortWords作为结束函数,所以我不想将它传递给另一个能够执行该take函数.如果有一种方法可以take在调用之前执行,sortWords这可能是一个解决方案,但是我也尝试了这一点,并发现所采取的单词不是先排序,因此不会给我我想要的结果.
谢谢
你将如何实现take列表理解?
到目前为止我的方法:
take2 :: (Num i, Ord i) => i -> [a] -> [a]
take2 n xs = [x | x <- xs, [x..n]]
Run Code Online (Sandbox Code Playgroud) 嗨,我是 clojure 新手,
我正在尝试创建一个函数,该函数将集合拆分为大小不断增加的块
(apply #(#(take %) (range 1 n)) col)
Run Code Online (Sandbox Code Playgroud)
其中 n 是块的数量
预期输出示例:n = 4 且 col =(范围 1 4)
(1) (2 3) (4)
Run Code Online (Sandbox Code Playgroud)
n = 7 和 col =(范围 1 7)
(1) (2 3) (4 5 6) (7)
Run Code Online (Sandbox Code Playgroud) 如果我知道集合中只有一个匹配的项目,是否有任何方法可以告诉Linq这样,以便在找到它时中止搜索?
我假设这两个在返回一个项目之前搜索完整的集合?
var fred = _people.Where((p) => p.Name == "Fred").First();
var bill = _people.Where((p) => p.Name == "Bill").Take(1);
Run Code Online (Sandbox Code Playgroud)
编辑:人们似乎注意到FirstOrDefault,或SingleOrDefault.这些与我的问题无关.如果集合为空,它们只是提供默认值.正如我所说,我知道我的收藏品有一个匹配的项目.
AakashM的评论对我来说最感兴趣.我觉得我的假设是错的,但我很感兴趣.例如,当linq to objects在我的示例代码中运行Where()函数时,它如何知道对其返回值有进一步的操作?
我有一个字符串,其中包含 45f5f5ca1997b35bf09b8e0932b0d693967d0608a3c
如何获取该字符串的前 16 个字节?