haskell

Is there a way to place some impure code inside pure functions?

爱⌒轻易说出口 提交于 2021-02-01 05:19:23
问题 IO , just like Maybe , is just an instance of Monad . On the other hand we have all data constructors for Maybe ( Just and Nothing ), but no constructors for IO . Reader and Writer do not export constructors too, they have functions, which return instance of this type ( reader and writer ) and more importantly runReader and runWriter , which unwrap computation result from Monad. Is there a way to unwrap IO Monad? I would like to have pure function which do some impure IO computations under

Is there a way to place some impure code inside pure functions?

偶尔善良 提交于 2021-02-01 05:19:10
问题 IO , just like Maybe , is just an instance of Monad . On the other hand we have all data constructors for Maybe ( Just and Nothing ), but no constructors for IO . Reader and Writer do not export constructors too, they have functions, which return instance of this type ( reader and writer ) and more importantly runReader and runWriter , which unwrap computation result from Monad. Is there a way to unwrap IO Monad? I would like to have pure function which do some impure IO computations under

Haskell Display String into 8 columns

不问归期 提交于 2021-01-29 21:37:14
问题 I have type person = (String, Float, Float, [Int]) ** name, height, weight, miles walked past week** The [Int] will contain data for how many miles the person walked on each day for the past 7 days e.g. [5,8,12,7,12,6,9] My testData consists of multiple peoples data and I want to return all names and the 7 daily figures of the miles they walked as a single string, neatly into separate rows for each person and have the miles walked data lined up in columns. e.g. testData = [(John, 1.76, 63, [5

Testing empty list [] with Eq type

让人想犯罪 __ 提交于 2021-01-29 13:58:04
问题 Currently, I am writing a function in Haskell to check a list is symmetric or not. isReflexive :: Eq a => [(a, a)] -> Bool isReflexive [] = True isReflexive xs = and [elem (x, x) xs | x <- [fst u | u <- xs] ++ [snd u | u <- xs]] test = do print(isReflexive []) main = test The function works fine on the list that is not empty. However, when I test the empty list with the function, it raised an error Ambiguous type variable ‘a2’ arising from a use of ‘isReflexive’ prevents the constraint ‘(Eq

Cabal install tidal ends with warning

亡梦爱人 提交于 2021-01-29 09:00:53
问题 I'm trying install tidal in command line this way: cabal install tidal but it ends with this message: Warning: You asked to install executables, but there are no executables in target: tidal. Perhaps you want to use --lib to install libraries instead. Return of: cabal install tidal --lib is: Resolving dependencies... Up to date If I check ghk-pkg list, there is no package tidal ... Have somebody similar problem or what I'm doing wrong? My environment is: Windows 10 Education Haskell 8.4.3

Haskell: Problem Importing System.Random Windows

北城余情 提交于 2021-01-29 05:58:32
问题 I've been trying use the System.Random library but keep getting the error Could not find module `System.Random' Use -v (or `:set -v` in ghci) to see a list of the files searched for. import System.Random I've installed random with cabal (cabal install --lib random) and in powershell it says that everything is up to date. I've tried installing stack to fix the problem but have come across a lot of problems doing that. 来源: https://stackoverflow.com/questions/65250834/haskell-problem-importing

How to apply polymorphic function to a concrete type?

微笑、不失礼 提交于 2021-01-29 04:14:39
问题 Below is a distilled version of a problem I encountered while learning Haskell: data A = A data B = B data Test = TestA A | TestB B test :: (a -> a) -> (Test -> Test) test op t = case t of TestA a -> TestA $ op a TestB b -> TestB $ op b testA = test id (TestA A) testB = test id (TestB B) Trying to compile this gives the following error: Couldn't match expected type ‘B’ with actual type ‘a’ ‘a’ is a rigid type variable bound by the type signature for test :: (a -> a) -> Test -> Test What's

How does the “monad-based IO” design of Haskell contrasts with one based on tagging pure/impure code?

妖精的绣舞 提交于 2021-01-29 02:37:58
问题 The IO Monad is known to separate pure from impure code in Haskell, such that a signature like f :: ... -> IO ? represents an action with side-effects, otherwise it is a pure function. In addition, monads are used in Haskell as a purely functional mechanism to sequence actions, which otherwise could be unexpectedly reordered by a compiler that is designed to deal with only pure functions. The trouble with a design around monads is that they seem hard to understand to many, perhaps including

How does `[ (x !! 0, x !! 1) | x <- mapM (const ['A', 'B', 'C'] ) [1..2], head x < head (tail x) ]` work?

∥☆過路亽.° 提交于 2021-01-28 17:56:36
问题 I am new to Haskell and wondering how the statement [ (x !! 0, x !! 1) | x <- mapM (const ['A', 'B', 'C'] ) [1..2], head x < head (tail x) ] works. (I found it on StackOverflow.) I know what it outputs, but I am not really understanding it. 回答1: Well the above expression is likely not what is considered idiomatic Haskell. Probably a better version would be: [ (x0, x1) | (x0:x1:_) <- mapM (const "ABC") [1..2], x0 < x1 ] This is cleaner and if the lists in the mapM (const "ABC") would return a

How to apply a traversable of functions to a one value

放肆的年华 提交于 2021-01-28 17:49:28
问题 What should I use if I want to have something like [a->b] -> a -> [b] basically I have a list of functions, all take in a value a and returns b . I want to apply all of them to one a and get the results [b] . Which one should I use? Thanks 回答1: You don't need Traversable , just Functor : swingMap f x = fmap ($ x) f See also the swing function (this is equivalent to swing fmap ). Or, if you're using Edward Kmett's distributive library, you can have the best of both this answer (only a Functor