haskell

The Haskell way to do IO Loops (without explicit recursion)?

眉间皱痕 提交于 2020-01-24 06:45:07
问题 I want to read a list of strings seperated by newlines from STDIN, until a new line is witnessed and I want an action of the type IO [String] . Here is how I would do it with recursion: myReadList :: IO String myReadList = go [] where go :: [String] -> IO [String] go l = do { inp <- getLine; if (inp == "") then return l; else go (inp:l); } However, this method of using go obscures readability and is a pattern so common that one would ideally want to abstract this out. So, this was my attempt:

Transitive closure from a list using Haskell

岁酱吖の 提交于 2020-01-24 05:17:25
问题 I need to produce transitive closure on list using Haskell. So far I got this: import Data.List qq [] = [] qq [x] = [x] qq x = vv (sort x) vv (x:xs) = [x] ++ (member [x] [xs]) ++ (qq xs) member x [y] = [(x1, y2) | (x1, x2) <- x, (y1, y2) <- qq (y), x2 == y1] Output 1: *Main> qq [(1,2),(2,3),(3,4)] [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] Output 2: *Main> qq [(1,2),(2,3),(3,1)] [(1,2),(1,3),(1,1),(2,3),(2,1),(3,1)] The problem is with second output. Instead of checking for additional transitive

Reading GraphML in Haskell

人盡茶涼 提交于 2020-01-24 04:31:10
问题 I'm trying to read a GraphML file containing a single directed Graph into a Haskell Data.Graph in order to run an analysis using the Math.Combinatorics.Graph module. However, I can't find any module that allows me to read a GraphML file, producing a Data.Graph . One related module I found is ForSyDe.Backend.GraphML. However, this seems to be specific to the ForSyDe DSL and I currently can't think of a way to use it to read a plain Data.Graph . Could you point me to a library allowing me to

Strongly typed events in Haskell

纵饮孤独 提交于 2020-01-24 04:11:18
问题 I'm working on my first 'real' Haskell project, and simultaneously trying to get my head around event sourcing. (It seemed like a good match; event sourcing is a rather functional way of looking at data.) I've hit a wall trying to figure out how to deserialise my events into strongly typed Haskell data. There are two opposing forces at work here: It shouldn't be possible to apply an event to the wrong type of aggregate. This requirement suggests that I need a separate type of event for each

Haskell - is this a closure?

我是研究僧i 提交于 2020-01-24 03:10:33
问题 There is a piece of source-code that originated in an answer to another one of my questions, infFromPrefix :: Eq a => ([a] -> [a]) -> [a] -> [a] infFromPrefix rules prefix = inf where inf = prefix ++ case stripPrefix prefix (rules inf) of Just suffix -> suffix Nothing -> error "Substitution does not preserve prefix" where I am pretty sure that inf must be a closure as it has access to variables from its enclosing scope in the sense that it uses the parameters passed to infFromPrefix , but am

Two functions compile with type annotations. Remove one annotation - doesn't compile. Remove two - compiles again. Why?

南楼画角 提交于 2020-01-24 03:08:26
问题 Mind this Reflex program: {-# LANGUAGE ScopedTypeVariables, RecursiveDo #-} import Control.Applicative import Control.Monad import Control.Monad.IO.Class import Prelude hiding (div) import Reflex.Dom import qualified Data.Map as M clickMe :: MonadWidget t m => m (Event t ()) clickMe = do rec (e,_) <- elAttr' "button" M.empty (display c) c :: Dynamic t Int <- count (domEvent Click e) return $ domEvent Click e div :: forall t m a . MonadWidget t m => m a -> m a div = elAttr "div" ("style" =:

IEEE floating point signalling NaN (sNaN) in Haskell

▼魔方 西西 提交于 2020-01-24 03:02:09
问题 Is there any way to define signaling NaN in Haskell? I found two approaches to deal with NaNs: 1) use 0/0, which produces quite nan 2) package Data.Number.Transfinite, which has no signaling NaNs too. PS Is there any way to put Word64 bit by bit into Double without writing C library? 回答1: I have found one non-portable way: {-# LANGUAGE ForeignFunctionInterface #-} import Data.Word (Word64, Word32) import Unsafe.Coerce import Foreign import Foreign.C.Types foreign import ccall "fenv.h

Equality on constraints

守給你的承諾、 提交于 2020-01-24 02:55:27
问题 Basically, given {-# LANGUAGE PolymorphicKinds, ConstraintKinds, TypeFamilies #-} (and more, if necessary), does the (~) type-level operator work on type-level expressions of kind Constraint ? I tried googling the answer, but had no luck. 回答1: Yes, it is possible. Because types of kind Constraint are finite sets of atomic type constraints, you can test their equality very easily. The PolyKinds extension is not necessary, however. Also, there's very few situations when this kind equality would

How is this function equivalent to getting the last item in a list?

删除回忆录丶 提交于 2020-01-24 02:50:11
问题 After completing the first problem (" Find the last element of a list ") in the 99 questions exercises, I wanted to see how my solution compared with others and I found this solution. myLast' = foldr1 (const id) This documentation seems to show that foldr1 takes two arguments, the first being a function and the second being a list. But this definition only appears to take a function as an argument. Is there an implicit definition of the arguments passed like this? myLast' xs = foldr1 (const

Could not deduce (Semigroup (Optional a)) arising from the superclasses of an instance declaration

一曲冷凌霜 提交于 2020-01-24 02:36:07
问题 The following code from "Haskell Programming: From first principles" fails to compile: module Learn where import Data.Semigroup import Data.Monoid -- Exercise: Optional Monoid data Optional a = Nada | Only a deriving (Eq, Show) instance Monoid a => Monoid (Optional a) where mempty = Nada mappend Nada Nada = Nada mappend (Only a) Nada = Only $ mappend a mempty mappend Nada (Only a) = Only $ mappend mempty a mappend (Only a) (Only b) = Only $ mappend a b It gives the following error: