haskell

GHC heap profiling with -hy - What is * (star)?

你离开我真会死。 提交于 2020-01-03 18:25:07
问题 When I profile my program's heap usage with -hy flag like ./prog +RTS -hy I often see the constructor * in the results, along with other constructors such as [] and Word8 . What is the type * in this context? Is it related to kinds ? 回答1: Quoted from Real World Haskell: There's also some heap allocated data of unknown type (represented as data of type "*"). And in the GHC User's Guide: For closures which have function type or unknown/polymorphic type, the string will represent an

Getting stack overflow error when trying Haskell-style lazy evaluation in Scala

你说的曾经没有我的故事 提交于 2020-01-03 17:17:07
问题 To practice, I'm writing some useless methods/functions in Scala. I'm trying to implement a fibonacci sequence function. I wrote one in Haskell to use as a reference (so I don't just end up writing it Java-style). What I've come up with in Haskell is: fib a b = c : (fib b c) where c = a+b Then I can do this: take 20 (fib 0 1) [1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946] So I tried translating this to Scala: def fib(a:Int, b:Int):List[Int] = { val c = a+b c :: fib(b

Haskell Servant Get Current Route / URL From Handler

折月煮酒 提交于 2020-01-03 17:08:22
问题 I'd like to get current route that corresponds to my handler. Here is mockup of my server just for reference: type ServerAPI = "route01" :> Get '[HTML] Text :<|> "route02" :> "subroute" :> Get '[HTML] Text :<|> "route03" :> Get '[HTML] Text And here are some handlers : route1and2Handler :: Handler Text route1and2Handler = do route <- getCurrentRoute addVisitCountForRouteToDatabaseOrSomethingOfThatSort... return template route3Handler :: Handler Text route3Handler = return "Hello, I'm route 03

Replacing do by >>= for a scotty post

有些话、适合烂在心里 提交于 2020-01-03 17:01:11
问题 post "/introduceAnIdea" $ do command <- jsonData json $ handle command How would you remove the do and change it with >>= ? 回答1: Here's how to rewrite do -notation as >>= and >> : (NB: a newline becomes ; in the c-like notation option, which I use here.) do { a <- m; b... } = m >>= \a -> do { b... } do { a; b... } = a >> do { b... } do { a } = a So this becomes: post "/introduceAnIdea" $ do { command <- jsonData; json $ handle command} = post "/introduceAnIdea" $ jsonData >>= \command -> do

How do you find the list of all numbers that are multiples of only powers of 2, 3, and 5? [duplicate]

别等时光非礼了梦想. 提交于 2020-01-03 16:51:20
问题 This question already has answers here : Generating integers in ascending order using a set of prime numbers (4 answers) Closed last year . I am trying to generate a list of all multiples which can be represented by the form , where a, b, and c are whole numbers. I tried the following, [ a * b * c | a <- map (2^) [0..], b <- map (3^) [0..], c <- map (5^) [0..] ] but it only lists powers of 5 and never goes on to 2 or 3. Edit: My apologies, it seems that I did not clarify the question enough.

Duplicate definition for symbol __module_registered error

大兔子大兔子 提交于 2020-01-03 16:48:14
问题 I get an error message from GHCi about a "duplicate definition for symbol __module_registered", like this: GHCi runtime linker: fatal error: I found a duplicate definition for symbol __module_registered whilst processing object file /usr/local/lib/ghc-6.2/HSfgl.o How to fix this? 回答1: Thats is easy, probably indicates that when building a library for GHCi (HSfgl.o in the above example), you should use the -x option to ld. 来源: https://stackoverflow.com/questions/3392925/duplicate-definition

Functions as arguments to be used in template haskell quote

十年热恋 提交于 2020-01-03 16:46:14
问题 This is partially a followup to Lift instance for a function?. However, the answer there is to either globally define the function or to rewrite it inside the quotation. However, we will be using foo a lot with different functions for f from within the scope of a let . This makes it just about impossible for us to define multiple global version of f. The latter solution, of writing our function within the quote, seem equivalent to writing a lift on functions. So, is there any way of lifting

Why is using QuantifiedConstraints to specify a subclass of a typeclass also demanding an instance of the subclass?

主宰稳场 提交于 2020-01-03 15:58:11
问题 I'm playing around with a multikinded tagless encoding of Free {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE QuantifiedConstraints #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TypeOperators #-} module Free where import GHC.Types type (a :: k) ~> (b :: k) = Morphism k a b newtype Natural (f :: j -> k) (g :: j -> k) = Natural {

Could not find module `Control.Monad.Reader'

我是研究僧i 提交于 2020-01-03 15:33:10
问题 Today when I tried to compile my code in Geany I got this error Could not find module `Control.Monad.Reader': it was found in multiple packages: monads-fd-0.1.0.1 mtl-1.1.0.2 Compilation failed. I get the same for the Writer monad; I thought I should remove one of those packages, but I do not want to break other packages, so now what should I do, yesterday everything worked without any problem. 回答1: It looks like you have recently installed monads-fd , perhaps as a dependency of something

apply function on Maybe types?

我只是一个虾纸丫 提交于 2020-01-03 14:56:26
问题 New to Haskell and I can't figure out how apply a function (a -> b) into a list [Maybe a] and get [Maybe b] maybeX:: (a -> b) -> [Maybe a] -> [Maybe b] The function is supposed to do the exact same thing as map, apply the function f on a list of Maybe statements and if it Just it returns me a f Just and if it's a Nothing just a Nothing. Like the following example I want to add +5 on every Element of the following List : [Just 1,Just 2,Nothing,Just 3] and get [Just 6,Just 7,Nothing,Just 8]