haskell

How do I make this algorithm lazier without repeating myself?

淺唱寂寞╮ 提交于 2020-01-03 10:05:39
问题 (Inspired by my answer to this question.) Consider this code (it's supposed to find the largest element that's less than or equal to a given input): data TreeMap v = Leaf | Node Integer v (TreeMap v) (TreeMap v) deriving (Show, Read, Eq, Ord) closestLess :: Integer -> TreeMap v -> Maybe (Integer, v) closestLess i = precise Nothing where precise :: Maybe (Integer, v) -> TreeMap v -> Maybe (Integer, v) precise closestSoFar Leaf = closestSoFar precise closestSoFar (Node k v l r) = case i

Template Haskell type quoting problems

丶灬走出姿态 提交于 2020-01-03 09:57:29
问题 The TemplateHaskell quoting documents two quotes ( '' ) as the way to get the Name of a type: > ''String GHC.Base.String This works fine for this type (name). However, I can't find a way to make it work nice for e.g. Maybe String : > ''Maybe String -- interprets String as a data constructor > ''Maybe ''String -- wants to apply ''String to the Name type I know I can workaround via using [t| Maybe String |] , but this is then in the Q monad, and requires type changes, and I think is not type

How do I avoid a dependency cycle when generating a list of recent posts on posts?

我是研究僧i 提交于 2020-01-03 09:45:34
问题 So this works: create ["archive.html"] $ do route idRoute compile $ do posts <- (myRecentFirst gitTimes) =<< loadAll "posts/**" let archiveCtx = listField "posts" (postCtx allTags allCategories gitTimes) (return posts) `mappend` constField "title" "Archives" `mappend` (postCtx allTags allCategories gitTimes) makeItem "" >>= loadAndApplyTemplate "templates/archive.html" archiveCtx >>= loadAndApplyTemplate "templates/default.html" archiveCtx >>= relativizeUrls to create a list of recent posts

How can I make data that is allocated manually be garbage-collected in Haskell?

浪子不回头ぞ 提交于 2020-01-03 09:45:25
问题 I'm thinking about a FFI calling some C functions from Haskell. If a memory buffer is used to hold some data and is allocated "manually" and then it is used in Haskell computations, can I somehow rely on the garbage collector to free it when it is not needed anymore. As for the manual allocations, there are basically two ways (but the difference doesn't seem to be essential for my question): allocating a buffer in Haskell, then passing it to C function, like in fdRead allocating a buffer in C

How can I detect if GHC is set to generate 32bit or 64bit code by default?

醉酒当歌 提交于 2020-01-03 09:03:11
问题 I have the following bits in my makefile: GLFW_FLAG := -m32 -O2 -Iglfw/include -Iglfw/lib -Iglfw/lib/cocoa $(CFLAGS) ... $(BUILD_DIR)/%.o : %.c $(CC) -c $(GLFW_FLAG) $< -o $@ $(BUILD_DIR)/%.o : %.m $(CC) -c $(GLFW_FLAG) $< -o $@ The -m32 instructs GCC to generate 32bit code. It's there because on some configurations GHC is set to build 32bit code but GCC's default is sometimes 64bit. I would like to generalize this so that it autodetects whether GHC is building 32bit or 64bit code and then

Memory usage of constructors in haskell [duplicate]

心已入冬 提交于 2020-01-03 08:54:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Memory footprint of Haskell data types When solving combinatorial problems, I will often represent the solution as a bit string, eg. 1010100010110111000110... You get the picture. I figured that when I use [Int] for the bit string, Int always spends the same amount of memory, no matter how big the number actually is (because Int it's bounded, in contrast to Integer ), as the computer only remembers the bit

Using Generic Deriving with a Record Haskell

泪湿孤枕 提交于 2020-01-03 08:20:09
问题 I am basically attempting to see if I can emulate an ORM framework within Haskell, so that if a user wants to make a database model, they would do something like this data Car = Car { company :: String, model :: String, year :: Int } deriving (Model) Where the table would be "Car", and the columns would be the company,model,year To do this within Haskell, you have to use a combination of classes and generics, and this is where I am getting stuck. Using this tutorial (http://www.haskell.org

Naming of `pure` function in Control.Applicative [closed]

我们两清 提交于 2020-01-03 07:42:24
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Why is the function for lifting a value into a functor named pure in Control.Applicative? 回答1: Think of pure as an adjective. foo <*> pure 4 = foo applied on a pure value 4 . (As for the exact reason why it's called pure , probably only McBride and Paterson will know.) 回答2: It's a little like fromInteger . Its

Data.ByteString.Lazy.Char8 newline conversion on Windows---is the documentation misleading?

心已入冬 提交于 2020-01-03 07:21:06
问题 I have a question about the Data.ByteString.Lazy.Char8 library in the bytestring library. Specifically, my question concerns the readFile function, which is documented as follows: Read an entire file lazily into a ByteString. Use 'text mode' on Windows to interpret newlines I'm interested in the claim that this function will 'use text mode on Windows to interpret newlines'. The source code for the function is as follows: -- | Read an entire file /lazily/ into a 'ByteString'. Use 'text mode' -

Can I avoid “rightward drift” in Haskell?

感情迁移 提交于 2020-01-03 07:18:12
问题 When I use an imperative language I often write code like foo (x) { if (x < 0) return True; y = getForX(x); if (y < 0) return True; return x < y; } That is, I check conditions off one by one, breaking out of the block as soon as possible. I like this because it keeps the code "flat" and obeys the principle of "end weight". I consider it to be more readable. But in Haskell I would have written that as foo x = do if x < 0 then return x else do y <- getForX x if y < 0 then return True else