haskell

Error haskell: not in scope. What does that mean?

萝らか妹 提交于 2019-12-31 20:18:06
问题 I started with Haskell today and all the functions I perform on ghci display this message. I just want to know why this is happening. I know there are a lot of questions about this, but this is a simple case and I need to understand this error in the beginning function3 :: Int -> [Int] function3 x = [a | a <- [1..x] mod a x == 0] 回答1: Did error happen when you type the function type in GHCi? $ ghci GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help Prelude> function3 :: Int -> [Int]

All combinations of elements of two lists in Haskell

回眸只為那壹抹淺笑 提交于 2019-12-31 19:28:51
问题 Given two lists, [a, b] and [c, d] , I'd like to get the following result: [(a,c), (a,d), (b,c), (b,d)] How can I do this in Haskell? Is there a built-in function for this, or should I implement one myself? 回答1: [ (x,y) | x<-[a,b], y<-[c,d] ] This doesn't really require any further explanation, does it? 回答2: Applicative style all the way! λ> :m + Control.Applicative λ> (,) <$> ['a','b'] <*> ['c','d'] [('a','c'),('a','d'),('b','c'),('b','d')] (I've eschewed any String syntactic sugar above, in

Cabal - Expose all modules while building library

孤街醉人 提交于 2019-12-31 19:02:49
问题 Is it possible to tell Cabal to expose all modules while building a library? Right now I have to provide very long list of modules in the Exposed-modules cabal configurtion file section. 回答1: The modern answer is stack + hpack instead of using explicit cabal config. It could automatically expose package modules and provides many other enhancements. 回答2: You have to list all modules in the cabal configuration file. In your case, you just put the list of modules after exposed-module: . There is

Cabal - Expose all modules while building library

删除回忆录丶 提交于 2019-12-31 19:02:33
问题 Is it possible to tell Cabal to expose all modules while building a library? Right now I have to provide very long list of modules in the Exposed-modules cabal configurtion file section. 回答1: The modern answer is stack + hpack instead of using explicit cabal config. It could automatically expose package modules and provides many other enhancements. 回答2: You have to list all modules in the cabal configuration file. In your case, you just put the list of modules after exposed-module: . There is

Haskell shying away from probabilistic data structures?

北城以北 提交于 2019-12-31 19:01:41
问题 If you search for skips lists implemented in Haskell, you won't find many. It is a probabilistic data structure needing a random number generator, meaning that any of these structures would need to run in the IO monad. Are Haskell folks staying away from these data structures because it's not possible to implement them purely? How can Haskell deal with them? 回答1: A pseudo-random number generator can of course be used outside of IO , by simply storing the current generator value along with the

Least-strict (*)

风流意气都作罢 提交于 2019-12-31 12:12:08
问题 Is it possible to implement (*) with least-strict semantics in Haskell (standardized Haskell preferred, but extensions are OK. Using compiler internals is cheating)? For example, such a definition should result in the following being true: 0 * ⊥ = 0 ⊥ * 0 = 0 and only: ⊥ * ⊥ = ⊥ I can build pattern matches that satisfy one of the above cases, but not both, because the zero check forces the value. 回答1: Yes, but only using constrained impurity. laziestMult :: Num a => a -> a -> a laziestMult a

Restricting string literals to Text only

拜拜、爱过 提交于 2019-12-31 11:00:10
问题 I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text , and therefore, using a string literal as a list of characters should result in a type error. It appears to be impossible to import the IsString class without also importing the String instance for that class. Does ghc provide some way for me to restrict

How to replace a string with another in haskell

☆樱花仙子☆ 提交于 2019-12-31 10:47:33
问题 I want to replace a string from an input file with a different string. I was searching for a method but it seems i can only alter the string character by character. For example in the my code below replace :: String -> String replace [] = [] replace (x:xs) = if x == '@' then 'y':replace xs --y is just a random char else x:replace xs searching :: String -> IO String searching filename = do text <- readFile filename return(replace text) main :: IO () main = do n <- searching "test.sf" writeFile

Closures (in Haskell)

大兔子大兔子 提交于 2019-12-31 10:41:32
问题 To me a Closure is a (nested?) function with co-located data. When you write software in Haskell and look it through afterwards, you frequently find closures that you have created unintentionally. I do not quite get this right for myself. In what situations would I intentionally want to code closures? After all, in all examples I find the amount of co-located data is trivial/small and thus it does not quite seem to me as if in practice that would ever justify their (intentional) creation. Is

Closures (in Haskell)

左心房为你撑大大i 提交于 2019-12-31 10:41:10
问题 To me a Closure is a (nested?) function with co-located data. When you write software in Haskell and look it through afterwards, you frequently find closures that you have created unintentionally. I do not quite get this right for myself. In what situations would I intentionally want to code closures? After all, in all examples I find the amount of co-located data is trivial/small and thus it does not quite seem to me as if in practice that would ever justify their (intentional) creation. Is