haskell

Exponents defaulting to Integer

此生再无相见时 提交于 2020-01-15 09:54:30
问题 I use (^) :: (Num a, Integral b) => a -> b -> a a lot to define constant factors or sizes. Problem is that GHC complains about defaulting to Integer . Now I know why this happens ... and I know that I can "just" write (x^(y::Int)) to get rid of the warning. But that looks just "ugly". Otoh living with the warnings is also not a great option. Same thing applies for (^^) :: (Integral b, Fractional a) => a -> b -> a and (**) :: Floating a => a -> a -> a is not usable to me. Anyone has a nice

haskell random instance of own datatype

别说谁变了你拦得住时间么 提交于 2020-01-15 09:48:08
问题 I am working on an Asteroid game made in Haskell with Graphics.gloss. Now I have defined a datatype for the asteroids like this: data Asteroid = Asteroid { asteroidPos:: Point, asteroidVel :: Vector, asteroidSize :: Float } So that it has a Position defined by a point, a velocity defined by a vector and it's size. Now I want to know how I could write an instance of Random for this datatype so that a new asteroid appears at a random time, on a random place with a random velocity. Does anyone

How do I use `break_` in the package “loops”?

故事扮演 提交于 2020-01-15 09:14:02
问题 Looking into the package loops, I found it very interesting and could be useful. However, there's one part about the package that I don't understand: how am I supposed to use break_? Let's say I have a function get' :: IO (Maybe Int) , with which each call will return a number read from a file, and returns Nothing if the EOF is reached. I'm trying to construct a simple loop where I print each number and break upon EOF. Now, I know to loop indefinitely I can use forever : import Control.Monad

How do I use `break_` in the package “loops”?

五迷三道 提交于 2020-01-15 09:13:21
问题 Looking into the package loops, I found it very interesting and could be useful. However, there's one part about the package that I don't understand: how am I supposed to use break_? Let's say I have a function get' :: IO (Maybe Int) , with which each call will return a number read from a file, and returns Nothing if the EOF is reached. I'm trying to construct a simple loop where I print each number and break upon EOF. Now, I know to loop indefinitely I can use forever : import Control.Monad

How to make nicEditor snaplet? (Several questions)

我的梦境 提交于 2020-01-15 08:01:12
问题 The example below defines a snaplet to bind nicEditor to textarea. The following questions are not only related to the example below, but probably they are related to some other similar cases.. Can a newbie follow the instructions below (how to clarify)? How to make the example use less steps or otherwise simpler? (Is it possible with approximately the same content as below?) This used interpreted splice. If possible, should a snaplet provide compiled splices, too? A snaplet probably could

How to define default implementation in subclass definition in Haskell?

天涯浪子 提交于 2020-01-15 07:02:14
问题 I am new comer of Haskell and following is my question: given this class: class MyClass a where foo :: a -> [a] then I have a subclass that is more specific: class (MyClass a) => SubClass a where foo param = [bar param] bar :: a -> a but it doesn't work as expected. I was expecting a default implementation is setup in the definition of SubClass but it doesn't. I will need to define the instance for MyClass seperately, but that sounds stupid. How can I achieve default implementation when I

How to define default implementation in subclass definition in Haskell?

痴心易碎 提交于 2020-01-15 07:02:12
问题 I am new comer of Haskell and following is my question: given this class: class MyClass a where foo :: a -> [a] then I have a subclass that is more specific: class (MyClass a) => SubClass a where foo param = [bar param] bar :: a -> a but it doesn't work as expected. I was expecting a default implementation is setup in the definition of SubClass but it doesn't. I will need to define the instance for MyClass seperately, but that sounds stupid. How can I achieve default implementation when I

HXT getting first element: refactor weird arrow

我是研究僧i 提交于 2020-01-15 06:43:23
问题 I need to get text contents of first <p> which is children of <div class="about"> , wrote the following code: tagTextS :: IOSArrow XmlTree String tagTextS = getChildren >>> getText >>> arr stripString parseDescription :: IOSArrow XmlTree String parseDescription = ( deep (isElem >>> hasName "div" >>> hasAttrValue "id" (== "company_about_full_description")) >>> (arr (\x -> x) /> isElem >>> hasName "p") >. (!! 0) >>> tagTextS ) `orElse` (constA "") Look at this arr (\x -> x) – without it I wasn

the Coverage Condition fails

断了今生、忘了曾经 提交于 2020-01-15 05:45:06
问题 I have a very simple piece of code as follow: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts #-} class Graph g n e | g -> n e where nodes :: g -> [n] edge :: g -> (n,n) -> Maybe e instance Graph g Int e where nodes g = [] edge g (n1,n2) = Nothing I got an error related to the Coverage Condition fails for one of the functional dependencies. Do I need to add -XUndecidableInstances to permit this? or how I can fix this problem? Thanks 回答1: The

recursive update a “Behaviour” in Sodium yields 'thread blocked …'

孤街浪徒 提交于 2020-01-15 05:41:28
问题 i would update a Behaviour (Cell / Val) from it's current value. but the following code throws a thread blocked indefinitely in an MVar operation exception. i have expected it prints three times 'value of i: '. what did i missing? - thanks. {-# LANGUAGE RecursiveDo #-} module Main where import FRP.Sodium main :: IO () main = do (e, t) <- sync newEvent rec b <- sync $ hold 0 $ snapshot (\_ i -> i + 1) e b sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i) sync $ t "ping" sync