The following yields a deadlock error message (* Exception: thread blocked indefinitely in an MVar operation). I've thought it through step by step, and I don't see the issue. On the main thread, a MVar is created, and given to producer, running on a new thread producer starts, and blocks at listenOn, waiting for a connection The main thread continues into the loop, and blocks, wai
以下内容会产生死锁错误消息(*异常:MVar操作中无限期阻塞的线程)。 我一直在想,它并没有看到这个问题。 在主线程上,创建一个MVar,并将其交给生产者,并在新线程上运行 制片人开始,并在listenOn上阻止,等待连接 主线程继续进入循环,并阻塞,等待MVar接收一些东西 一旦生产者获得连接,它就会继续进入它的循环,并且在从套接字接收到一些东西后,将它放入MVar中 意义(据我了解),它应该最终与生产者在MVar中
I always thought that Haskell would do some sort of automatic intelligent memoizing. Eg, the naive Fibonacci implementation fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) would be fast because of that. Now I read this and it seems I was wrong -- Haskell doesn't seem to do automatic memoization. Or do I understand something wrong? Are there other languages which do automatic (ie impl
我一直认为Haskell会做某种自动智能记忆。 例如,天真的斐波那契实现 fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) 因此会很快。 现在我读了这个,似乎我错了 - Haskell似乎没有做自动记忆。 或者我理解错了什么? 是否有其他语言自动执行(即隐式的,而不是显式的)记忆? 实现记忆的常用方法是什么? 在我看到的所有示例实现中,它们都使用hashmap,但其大小没有任何限制。 显然,这在实践中不起作用,因为
Going through Project Euler I am comparing my solutions to the ones here. For question 8 my code produces the correct answer (confirmed via the check sum on the website) 23514624000. module Main where import Data.List main = do print $ last (sort eulerEight) eulerEight = doCalc [ x | x <- toDigits 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848
浏览欧拉项目我在这里比较我的解决方案。 对于问题8,我的代码产生了正确的答案(通过网站上的支票总额确认)23514624000。 module Main where import Data.List main = do print $ last (sort eulerEight) eulerEight = doCalc [ x | x <- toDigits 731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125
I am very new to Haskell and i thought that to get a hang of writing haskell programs,i might solve some project euler problems. So i went on with it and implemented the problem number 4 of Project Euler. The problem Statement: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome
我对Haskell非常陌生,我认为为了得到编写haskell程序的一窍不通,我可能会解决一些项目问题。 所以我继续进行并实施了欧拉项目的第四个问题。 问题陈述: 回文数字读取相同的方式。 由两个2位数字产品制成的最大回文是9009 = 91×99。 查找由两个3位数字产品制成的最大回文。 但是我的解决方案似乎有些问题。 这里是: projectEuler4 :: (Ord a,Num a) => a projectEuler4 = max palindromeList where palin
I am currently learning functional programming and Haskell coming from a python background. To help me learn, I decided to do some project euler problems (http://projecteuler.net/problem=18). Currently I am on #18. Starting with this string, "75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32
我目前正在学习函数式编程,Haskell来自python背景。 为了帮助我学习,我决定做一些项目问题(http://projecteuler.net/problem=18)。 目前我在#18。 从这个字符串开始, "75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57
import Data.Set euler :: Int euler = sum [ x | x <- nums ] where nums = Data.Set.toList (Data.Set.union (Data.Set.fromList [3,6..999]) (Data.Set.fromList [5,10..999])) I am learning Haskell and hope you don't mind me asking this. Is there a nicer way to get a list holding all natural numbers below one thousand that are multiples of 3 or
import Data.Set euler :: Int euler = sum [ x | x <- nums ] where nums = Data.Set.toList (Data.Set.union (Data.Set.fromList [3,6..999]) (Data.Set.fromList [5,10..999])) 我正在学习Haskell,并希望你不介意我问这个问题。 有没有更好的方法来获得一个列表,其中所有自然数都低于1000,是3或5的倍数? (例如用zip或地图?) 编辑: import Data.List eul
I'm trying to get my head around F-algebras, and this article does a pretty good job. I understand the notion of a dual in category theory, but I'm having a hard time understanding how F-coalgebras (the dual of F-algebras) relate to lazy data structures in Haskell. F-algebras are described with an endofunctor with the function: F a -> a, which makes sense if you think of F a as an e
我试图让我的脑袋围绕F-代数,而这篇文章做得很好。 我理解分类理论中的双重概念,但我很难理解F-coalgebras(F-代数的对偶)如何与Haskell中的懒惰数据结构相关。 用代数函数描述F-代数的函数:F a - > a,如果将F a看作一个表达式,并且作为评估该表达式的结果,就像链接文章解释它一样,这是有意义的。 作为F-代数的对偶,F-代数的相应函数是a→F a。 维基百科说,F-代数可以用来创建无限的,懒惰的数据结构。 a -
If you were writing a bioinformatics algorithm in Haskell, you'd probably use an algebraic data type to represent the nucleotides: data Nucleotide = A | T | C | G You'd do similarly in Standard ML or OCaml, I assume (I've never really used either). A value of type Nucleotide can clearly be contained in two bits. However, doing so would cause access times to be slower than if you
如果您在Haskell中编写生物信息学算法,您可能会使用代数数据类型来表示核苷酸: data Nucleotide = A | T | C | G 你会在Standard ML或OCaml中做类似的事情,我假设(我从来没有真正使用过)。 Nucleotide类型的值可以清楚地包含在两位中。 但是,这样做会导致访问时间比使用每个Nucleotide值一个字节的速度慢,因为您需要使用二元运算符选择两个感兴趣的位。 因此,在决定如何表示代数数据类型时,编译器必须在内存效率
How can I find the actual amount of memory required to store a value of some data type in Haskell (mostly with GHC)? Is it possible to evaluate it at runtime (eg in GHCi) or is it possible to estimate memory requirements of a compound data type from its components? In general, if memory requirements of types a and b are known, what is the memory overhead of algebraic data types such as: data
我如何找到在Haskell中存储某种数据类型的值所需的实际内存量(主要是使用GHC)? 是否可以在运行时对其进行评估(例如,在GHCi中)还是可以从其组件评估复合数据类型的内存需求? 通常,如果类型a和b内存需求是已知的,代数数据类型的内存开销是多少,例如: data Uno = Uno a data Due = Due a b 例如,这些值占用内存中有多少字节? 1 :: Int8 1 :: Integer 2^100 :: Integer x -> x + 1 (1 :: Int8, 2 :: Int8) [1]
Recently, blog entries such as Computing the Size of a Hashmap explained how to reason about space complexities of commonly used container types. Now I'm facing the question of how to actually "see" which memory layout my GHC version chooses (depending on compile flags and target architecture) for weird data types (constructors) such as data BitVec257 = BitVec257 {-# UNPACK #-} !W
最近,诸如计算散列表大小的博客条目解释了如何推理常用容器类型的空间复杂性。 现在我面临的问题是如何实际“查看”我GHC版本选择哪种内存布局(取决于编译标志和目标体系结构)奇怪的数据类型(构造函数),例如 data BitVec257 = BitVec257 {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 {-# UNPACK #-} !Bool {-# UNPACK #-} !Word64