Search Results

Search found 4547 results on 182 pages for 'haskell io'.

Page 12/182 | < Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >

  • Haskell optimization of the following function

    - by me2
    Profiling of some code of mine showed that about 65% of the time I was running the following code. What it does is use the Data.Binary.Get monad to walk through a bytestring looking for the terminator. If it detects 0xff, it checks if the next byte is 0x00. If it is, it drops the 0x00 and continues. If it is not 0x00, then it drops both bytes and the resulting list of bytes is converted to a bytestring and returned. Any obvious ways to optimize this code? I can't see it. parseECS = f [] False where f acc ff = do b <- getWord8 if ff then if b == 0x00 then f (0xff:acc) False else return $ L.pack (reverse acc) else if b == 0xff then f acc True else f (b:acc) False

    Read the article

  • Haskell: Pattern Matching with Lists

    - by user1670032
    I'm trying to make a function that takes in a list, and if one of the elements is negative, then any elements in that list that are equal to its positive counterpart should be changed to 0. Eg, if there is a -2 in a list, then all 2's in that list should be changed to 0. Any ideas why it only works for some cases and not others? I'm not understanding why this is, I've looked it over several times. changeToZero [] = [] changeToZero [x] = [x] changeToZero (x:zs:y:ws) | (x < 0) && ((-1)*(x) == y) = x : zs : 0 : changeToZero ws changeToZero (x:xs) = x : changeToZero xs *Main changeToZero [-1,1,-2,2,-3,3] [-1,1,-2,2,-3,3] *Main changeToZero [-2,1,2,3] [-2,1,0,3] *Main changeToZero [-2,1,2,3,2] [-2,1,0,3,2] *Main changeToZero [1,-2,2,2,1] [1,-2,2,0,1]

    Read the article

  • Creating a Haskell Empty Set

    - by mvid
    I am attempting to pass back a Node type from this function, but I get the error that empty is out of scope: import Data.Set (Set) import qualified Data.Set as Set data Node = Vertex String (Set Node) deriving Show toNode :: String -> Node toNode x = Vertex x empty What am I doing wrong?

    Read the article

  • Haskell type classes and type families (cont'd)

    - by Giuseppe Maggiore
    I need some help in figuring a compiler error which is really driving me nuts... I have the following type class: infixl 7 --> class Selectable a s b where type Res a s b :: * (-->) :: (CNum n) => (Reference s a) -> (n,(a->b),(a->b->a)) -> Res a s b which I instance twice. First time goes like a charm: instance Selectable a s b where type Res a s b = Reference s b (-->) (Reference get set) (_,read,write) = (Reference (\s -> let (v,s') = get s in (read v,s')) (\s -> \x -> let (v,s') = get s v' = write v x (_,s'') = set s' v' in (x,s''))) since the type checker infers (-->) :: Reference s a -> (n,a->b,a->b->a) -> Reference s b and this signature matches with the class signature for (--) since Res a s b = Reference s b Now I add a second instance and everything breaks: instance (Recursive a, Rec a ~ reca) => Selectable a s (Method reca b c) where type Res a s (Method reca b c) = b -> Reference s c (-->) (Reference get set) (_,read,write) = \(x :: b) -> from_constant( Constant(\(s :: s)-> let (v,s') = get s :: (a,s) m = read v ry = m x :: Reference (reca) c (y,v') = getter ry (cons v) :: (c,reca) v'' = elim v' (_,s'') = set s' v'' in (y,s''))) :: Reference s c the compiler complains that Couldn't match expected type `Res a s (Method reca b c)' against inferred type `b -> Reference s c' The lambda expression `\ (x :: b) -> ...' has one argument, which does not match its type In the expression: \ (x :: b) -> from_constant (Constant (\ (s :: s) -> let ... in ...)) :: Reference s c In the definition of `-->': --> (Reference get set) (_, read, write) = \ (x :: b) -> from_constant (Constant (\ (s :: s) -> ...)) :: Reference s c reading carefully the compiler is telling me that it has inferred the type of (--) thusly: (-->) :: Reference s a -> (n,a->(Method reca b c),a->(Method reca b c)->a) -> (b -> Reference s c) which is correct since Res a s (Method reca b c) = b -> Reference s c but why can't it match the two definitions? Sorry for not offering a more succint and standalone example, but in this case I cannot figure how to do it...

    Read the article

  • Using an element against an entire list in Haskell

    - by Snick
    I have an assignment and am currently caught in one section of what I'm trying to do. Without going in to specific detail here is the basic layout: I'm given a data element, f, that holds four different types inside (each with their own purpose): data F = F Float Int, Int a function: func :: F -> F-> Q Which takes two data elements and (by simple calculations) returns a type that is now an updated version of one of the types in the first f. I now have an entire list of these elements and need to run the given function using one data element and return the type's value (not the data element). My first analysis was to use a foldl function: myfunc :: F -> [F] -> Q myfunc y [] = func y y -- func deals with the same data element calls myfunc y (x:xs) = foldl func y (x:xs) however I keep getting the same error: "Couldn't match expected type 'F' against inferred type 'Q'. In the first argument of 'foldl', namely 'myfunc' In the expression: foldl func y (x:xs) I apologise for such an abstract analysis on my problem but could anyone give me an idea as to what I should do? Should I even use a fold function or is there recursion I'm not thinking about?

    Read the article

  • Explain Type Classes in Haskell

    - by Tsubasa Gomamoto
    Hi, I am a C++ / Java programmer and the main paradigm I happen to use in everyday programming is OOP. In some thread I read a comment that Type classes are more intuitive in nature than OOP. Can someone explain the concept of type classes in simple words so that an OOP guy like me can understand it?

    Read the article

  • Haskell: Gluing a char and a list together?

    - by Vincent
    So I have this code here: toWords :: String - [a] toWords "" = [] toWords (nr1 : rest) | nr1 == ' ' = toWords rest | otherwise = nr1 : toWords rest The "toWords" function should simply remove all spaces and return a list with all the words. But I keep getting this error: test.hs:5:18: Couldn't match expected type a' against inferred typeChar' `a' is a rigid type variable bound by the type signature for `toWords' at test.hs:1:22 In the first argument of `(:)', namely `nr1' In the expression: nr1 : toWords rest In the definition of `toWords': toWords (nr1 : rest) | nr1 == ' ' = toWords rest | otherwise = nr1 : toWords rest Failed, modules loaded: none.

    Read the article

  • List of divisors of an integer n (Haskell)

    - by Code-Guru
    I currently have the following function to get the divisors of an integer: -- All divisors of a number divisors :: Integer -> [Integer] divisors 1 = [1] divisors n = firstHalf ++ secondHalf where firstHalf = filter (divides n) (candidates n) secondHalf = filter (\d -> n `div` d /= d) (map (n `div`) (reverse firstHalf)) candidates n = takeWhile (\d -> d * d <= n) [1..n] I ended up adding the filter to secondHalf because a divisor was repeating when n is a square of a prime number. This seems like a very inefficient way to solve this problem. So I have two questions: How do I measure if this really is a bottle neck in my algorithm? And if it is, how do I go about finding a better way to avoid repetitions when n is a square of a prime?

    Read the article

  • How and why is ap defined as liftM2 id in Haskell

    - by luke_randall
    Whilst trying to better understand Applicative, I looked at the definition of <*, which tends to be defined as ap, which in turn is defined as: ap :: (Monad m) => m (a -> b) -> m a -> m b ap = liftM2 id Looking at the type signatures for liftM2 and id, namely: liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r id :: a -> a I fail to understand how just by passing in id, the relevant part of the type signature seems to transform from (a1 -> a2 -> r) -> m a1 to m (a -> b). What am I missing here?

    Read the article

  • Haskell: Defaulting constraints to type

    - by yairchu
    Consider this example: applyKTimes :: Integral i => i -> (a -> a) -> a -> a applyKTimes 0 _ x = x applyKTimes k f x = applyKTimes (k-1) f (f x) applyThrice :: (a -> a) -> a -> a applyThrice = applyKTimes 3 The 3 in applyThrice is defaulted by GHC to an Integer as shown when compiling with -Wall: Warning: Defaulting the following constraint(s) to type 'Integer' 'Integral t' arising from a use of 'applyKTimes' So I guess that Integer is the default Integral a => a. Is there a way to define "default types" for other constraints too? Is using default types bad practice? (it does complain when using -Wall..)

    Read the article

  • Where can I learn advanced Haskell?

    - by FredOverflow
    In a comment to one of my answers, SO user sdcwc essentially pointed out that the following code: comb 0 = [[]] comb n = let rest = comb (n-1) in map ('0':) rest ++ map ('1':) rest could be replaced by: comb n = replicateM n "01" which had me completely stunned. Now I am looking for a tutorial, book or PDF that teaches these advanced concepts. I am not looking for a "what's a monad" tutorial aimed at beginners or online references explaining the type of replicateM. I want to learn how to think in monads and use them effectively, monadic "patterns" if you will.

    Read the article

  • Haskell map function with predicate

    - by Paul
    I feel like this should be fairly obvious, or easy, but I just can't get it. What I want to do is apply a function to a list (using map) but only if a condition is held. Imagine you only wanted to divide the numbers which were even: map (`div` 2) (even) [1,2,3,4] And that would give out [1,1,3,2] since only the even numbers would have the function applied to them. Obviously this doesn't work, but is there a way to make this work without having to write a seperate function that you can give to map? Filter is almost there, except I also want to keep the elements which the condition doesn't hold for, and just not apply the function to them. Thanks

    Read the article

  • Haskell: Problems with overloading: Interpreter can´t tell which + to use

    - by Ben
    Hi, I want to make functions Double - Double an instance of the Num typeclass. I want to define the sum of two functions as sum of their images. So I wrote instance Num Function where f + g = (\ x - (f x) + (g x)) Here the compiler complains he can´t tell whether I´m using Prelude.+ or Module.+ in the lambda expression. So I imported Prelude qualified as P and wrote instance Num Function where f + g = (\ x - (f x) P.+ (g x)) This compiles just fine, but when I try to add two functions in GHCi the interpreter complains again he can´t tell whether I´m using Prelude.+ or Module.+. Is there any way I can solve this problem?

    Read the article

  • Haskell: Constrain function on type Double to only work with Integers

    - by thurn
    Suppose I'm writing a function that takes a list of integers and returns only those integers in the list that are less than 5.2. I might do something like this: belowThreshold = filter (< 5.2) Easy enough, right? But now I want to constrain this function to only work with input lists of type [Int] for design reasons of my own. This seems like a reasonable request. Alas, no. A declaration that constraints the types as so: belowThreshold :: [Integer] -> [Integer] belowThreshold = filter (< 5.2) Causes a type error. So what's the story here? Why does doing filter (< 5.2) seem to convert my input list into Doubles? How can I make a version of this function that only accepts integer lists and only returns integer lists? Why does the type system hate me?

    Read the article

  • Point-free in Haskell

    - by wwrob
    I have this code that I want to make point-free; (\k t -> chr $ a + flip mod 26 (ord k + ord t -2*a)) How do I do that? Also are there some general rules for point free style other than "think about this amd come up with something"?

    Read the article

  • Problem when trying to define Show for my Point3D type in Haskell

    - by devoured elysium
    I am trying to define Show for my Point3D type: type Point3D = (Integer, Integer, Integer) instance Show Point3D where show (x,y,z) = "<" ++ (show x) ++ "," ++ (show y) ++ "," ++ (show z) ++ ">" yet I must be missing something in the sintax, as I am always getting an error: Illegal instance declaration for `Show Point3D' (All instance types must be of the form (T t1 ... tn) where T is not a synonym. Use -XTypeSynonymInstances if you want to disable this.) In the instance declaration for `Show Point3D' What am I doing wrong?

    Read the article

  • Linking/Combining Type Classes in Haskell

    - by thegravian
    Say I have two type classes defined as follows that are identical in function but different in names: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a class PhantomMonad p where pbind :: p a -> (a -> p b) -> p b preturn :: a -> p b Is there a way to tie these two classes together so something that is an instance of PhantomMonad will automatically be an instance of Monad, or will instances for each class have to be explicitly written? Any insight would be most appreciated, thanks!

    Read the article

  • lists searches in SYB or uniplate haskell

    - by Chris
    I have been using uniplate and SYB and I am trying to transform a list For instance type Tree = [DataA] data DataA = DataA1 [DataB] | DataA2 String | DataA3 String [DataA] deriving Show data DataB = DataB1 [DataA] | DataB2 String | DataB3 String [DataB] deriving Show For instance, I would like to traverse my tree and append a value to all [DataB] So my first thought was to do this: changeDataB:: Tree -> Tree changeDataB = everywhere(mkT changeDataB') chanegDataB'::[DataB] -> [DataB] changeDataB' <add changes here> or if I was using uniplate changeDataB:: Tree -> Tree changeDataB = transformBi changeDataB' chanegDataB'::[DataB] -> [DataB] changeDataB' <add changes here> The problem is that I only want to search on the full list. Doing either of these searches will cause a search on the full list and all of the sub-lists (including the empty list) The other problem is that a value in [DataB] may generate a [DataB], so I don't know if this is the same kind of solution as not searching chars in a string. I could pattern match on DataA1 and DataB3, but in my real application there are a bunch of [DataB]. Pattern matching on the parents would be extensive. The other thought that I had was to create a data DataBs = [DataB] and use that to transform on. That seems kind of lame, there must be a better solution.

    Read the article

< Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >