Search Results

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

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

  • Haskell parsec parsing a string of items

    - by Chris
    I have a list that I need to parse where the all but the last element needs to be parsed by one parser, and the last element needs to be parsed by another parser. a = "p1 p1b ... p2" or a = "p2" Originally I tried parser = do parse1 <- many parser1 parse2 <- parser2 return AParse parse1 parse2 The problem is that parse1 can consume a parse2 input. So parse1 always consumes the entire list, and leave parse2 with nothing. Is there a way to say to apply parse1 to everything besides the last element in a string, and then apply parse2?

    Read the article

  • Haskell compile time function calculation

    - by egon
    I would like to precalculate values for a function at compile-time. Example (real function is more complex, didn't try compiling): base = 10 mymodulus n = n `mod` base -- or substitute with a function that takes -- too much to compute at runtime printmodules 0 = [mymodulus 0] printmodules z = (mymodulus z):(printmodules (z-1)) main = printmodules 64 I know that mymodulus n will be called only with n < 64 and I would like to precalculate mymodulus for n values of 0..64 at compile time. The reason is that mymodulus would be really expensive and will be reused multiple times.

    Read the article

  • Parsec Haskell Lists

    - by Martin
    I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input and get a HTML output. If my input is: * First item, First level ** First item, Second level ** Second item, Second level * Second item, First level My output should be: <ul><li>First item, First level <ul><li>First item, Second level </li><li>Second item, Second level </li></ul></li><li>Second item, First level</li></ul> I wrote this, but obviously does not work recursively list= do{ s <- many1 item;return (olist << s) } item= do{ (count 1 (char '*')) ;s <- manyTill anyChar newline ;return ( li << s) } Any ideas? the recursion can be more than two levels Thanks!

    Read the article

  • Haskell optimization of a function looking for a bytestring terminator

    - by me2
    Profiling of some code showed that about 65% of the time I was inside 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? 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

  • Help with possible Haskell type inference quiz questions

    - by Linda Cohen
    foldr:: (a -> b -> b) -> b -> [a] -> b map :: (a -> b) -> [a] -> [b] mys :: a -> a (.) :: (a -> b) -> (c -> a) -> c -> b what is inferred type of: a.map mys :: b.mys map :: c.foldr map :: d.foldr map.mys :: I've tried to create mys myself using mys n = n + 2 but the type of that is mys :: Num a => a -> a What's the difference between Num a = a - a and just a - a? Or what does 'Num a =' mean? Is it that mys would only take Num type? So anyways, a) I got [a] - [a] I think because it would just take a list and return it +2'd according to my definition of mys b) (a - b) - [a] - [b] I think because map still needs take both arguments like (*3) and a list then returns [a] which goes to mys and returns [b] c) I'm not sure how to do this 1. d) I'm not sure how to do this 1 either but map.mys means do mys first then map right? Are my answers and thoughts correct? If not, why not? THANKS!

    Read the article

  • Parsec Haskell to HTML

    - by Martin
    I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: hello 123 --this is an emphasized text-- bye\n And my output should be: <p>hello 123 <em>this is an emphasized text</em> bye\n</p> Any ideas? Thanks!!

    Read the article

  • NILL value in haskell

    - by snorlaks
    Hello, I get input (x) from user, convert it to Int by let y = (read x)::Int and then I would like the function to behave in a special way if user gave nothing (empty string). -- In this place I would like to handle situation in which user -- gave empty string as argument -- this doesnt work :/ yearFilter [] y = True --This works fine as far as y is integer yearFilter x y | x == (objectYear y) = True | otherwise = False Thanks for help, Bye

    Read the article

  • Very slow guards in my monadic random implementation (haskell)

    - by danpriduha
    Hi! I was tried to write one random number generator implementation, based on number class. I also add there Monad and MonadPlus instance. What mean "MonadPlus" and why I add this instance? Because of I want to use guards like here: -- test.hs -- import RandomMonad import Control.Monad import System.Random x = Rand (randomR (1 ::Integer, 3)) ::Rand StdGen Integer y = do a <-x guard (a /=2) guard (a /=1) return a here comes RandomMonad.hs file contents: -- RandomMonad.hs -- module RandomMonad where import Control.Monad import System.Random import Data.List data RandomGen g => Rand g a = Rand (g ->(a,g)) | RandZero instance (Show g, RandomGen g) => Monad (Rand g) where return x = Rand (\g ->(x,g)) (RandZero)>>= _ = RandZero (Rand argTransformer)>>=(parametricRandom) = Rand funTransformer where funTransformer g | isZero x = funTransformer g1 | otherwise = (getRandom x g1,getGen x g1) where x = parametricRandom val (val,g1) = argTransformer g isZero RandZero = True isZero _ = False instance (Show g, RandomGen g) => MonadPlus (Rand g) where mzero = RandZero RandZero `mplus` x = x x `mplus` RandZero = x x `mplus` y = x getRandom :: RandomGen g => Rand g a ->g ->a getRandom (Rand f) g = (fst (f g)) getGen :: RandomGen g => Rand g a ->g -> g getGen (Rand f) g = snd (f g) when I run ghci interpreter, and give following command getRandom y (mkStdGen 2000000000) I can see memory overflow on my computer (1G). It's not expected, and if I delete one guard, it works very fast. Why in this case it works too slow? What I do wrong?

    Read the article

  • Understanding this matrix transposition function in Haskell

    - by dmindreader
    This matrix transposition function works, but I'm trying to understand its step by step execurtion and I don't get it. transpose:: [[a]]->[[a]] transpose ([]:_) = [] transpose x = (map head x) : transpose (map tail x) with transpose [[1,2,3],[4,5,6],[7,8,9]] it returns: [[1,4,7],[2,5,8],[3,6,9]] I don't get how the concatenation operator is working with map. It is concatenating each head of x in the same function call? How?

    Read the article

  • Haskell Ord instance with a Set

    - by mvid
    I have some code that I would like to use to append an edge to a Node data structure: import Data.Set (Set) import qualified Data.Set as Set data Node = Vertex String (Set Node) deriving Show addEdge :: Node -> Node -> Node addEdge (Vertex name neighbors) destination | Set.null neighbors = Vertex name (Set.singleton destination) | otherwise = Vertex name (Set.insert destination neighbors) However when I try to compile I get this error: No instance for (Ord Node) arising from a use of `Set.insert' As far as I can tell, Set.insert expects nothing but a value and a set to insert it into. What is this Ord?

    Read the article

  • installation HDBC-SQlite3 Haskell

    - by snorlaks
    Hello, Im facing the problem during installation : setup configure Configuring HDBC-sqlite3-2.3.0.0... setup: Missing dependency on a foreign library: * Missing C library: sqlite3 This problem can usually be solved by installing the system package that provides this library (you may need the "-dev" version). If the library is already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where it is. what should I do ? thanks for any help

    Read the article

  • haskell recursive function

    - by snorlaks
    Hello, Please help me with writing function which takes two arguments : list of ints and index (int) and returns list of integers with negative of value on specified index position in the table MyReverse :: [Int]-Int-[Int] for example myReverse [1,2,3,4,5] 3 = [1,2,-3,4,5] if index is bigger then length of the list or smaller then 0 return the same list. Thanks for help

    Read the article

  • Haskell - Parsec Parsing <p> element

    - by Martin
    I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: This is the first paragraph example\n with two lines\n \n And this is the second paragraph\n And my output should be: <p>This is the first paragraph example\n with two lines\n</p> <p>And this is the second paragraph\n</p> I defined: line= do{ ;t<-manyTill (anyChar) newline ;return t } paragraph = do{ t<-many1 (line) ;return ( p << t ) } But it returns: <p>This is the first paragraph example\n with two lines\n\n And this is the second paragraph\n</p> What is wrong? Any ideas? Thanks!

    Read the article

  • Problem understanding treesort in Haskell

    - by Jerry
    I am trying to figure out how exactly does treesort from here work (I understand flatten, insert and foldr). I suppose what's being done in treesort is applying insert for each element on the list thus generating a tree and then flattening it. The only problem I can't overcome here is where the list (that is the argument of the function) is hiding (because it is not written anywhere as an argument except for the function type declaration). One more thing: since dot operator is function composition, why is it an error when I change: treesort = flatten . foldr insert Leaf to treesort = flatten( foldr insert Leaf )?

    Read the article

  • Haskell - function (that returns a list) on each element in a list

    - by Ben
    The assignment is to create a multiples function and I essentially want todo the following code: map (\t -> scanl (\x y -> x+y) t (repeat t)) listofnumbers The problem is that the scanl function returns a list of results rather than the one which the map function requires. So is there a function that will allow the return of lists?

    Read the article

  • Haskell: variant of `show` that doesn't wrap String and Char in quotes

    - by Joey Adams
    I'd like a variant of show (let's call it label) that acts just like show, except that it doesn't wrap Strings in " " or Chars in ' '. Examples: > label 5 "5" > label "hello" "hello" > label 'c' "c" I tried implementing this manually, but I ran into some walls. Here is what I tried: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} module Label where class (Show a) => Label a where label :: a -> String instance Label [Char] where label str = str instance Label Char where label c = [c] -- Default case instance Show a => Label a where label x = show x However, because the default case's class overlaps instance Label [Char] and instance Label Char, those types don't work with the label function. Is there a library function that provides this functionality? If not, is there a workaround to get the above code to work?

    Read the article

  • The reason for MonadState get and put?

    - by CiscoIPPhone
    I'm reading the Monads chapter in Real World Haskell (chapter 14). A function is defined as follows: type RandomState a = State StdGen a getRandom :: Random a => RandomState a getRandom = get >>= \gen -> let (val, gen')= random gen in put gen' >> return val I don't really understand the purpose of the get and put functions here. I rewrote the function as following which seems to do the same thing and is more concise: getRandom2 :: Random a => RandomState a getRandom2= State $ \ s -> random s So my question is: What is the purpose of get and put?

    Read the article

  • Nicely printing/showing a binary tree in Haskell

    - by nicole
    I have a tree data type: data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a ...and I need to make it an instance of Show, without using deriving. I have found that nicely displaying a little branch with two leaves is easy: instance (Show a, Show b) => Show (Tree a b) where show (Leaf x) = show x show (Branch val l r) = " " ++ show val ++ "\n" ++ show l ++ " " ++ show r But how can I extend a nice structure to a tree of arbitrary size? It seems like determining the spacing would require me to know just how many leaves will be at the very bottom (or maybe just how many leaves there are in total) so that I can allocate all the space I need there and just work 'up.' I would probably need to call a size function. I can see this being workable, but is that making it harder than it is?

    Read the article

  • Manipulating source packages from Hackage how to easy deploy to several windowsboxes?

    - by Jonke
    Recently when I have found good sources packages for ghc 6.12/6.10 on Hackage I've been forced to do some minor or major changes to the cabal files to make those packages to work under windows. Besides to fork and merge my fixes with github, what seems to be the best way/ good enough practice to take these modified builds to a couple of other windows boxes that only has a basic haskell platform installed? I should prefer if I somehow could work with the cabal-install because that is what one normally use. Should one put the modfied build dirs on a shared/networked dir and mount from the targeted windows box? Say something like this: on machine prepare cabal fetch foo cabal unpack foo cd foo edit .cabal and .hs cabal configure cabal build On machine useanddevelopnormal cd machinepreparemount cd foo cabal install

    Read the article

  • Haskell maps returning a monad

    - by sabauma
    The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I have to use an older version of the library?

    Read the article

  • Consing lists with user-defined type in Haskell

    - by user1319603
    I have this type I defined myself: data Item = Book String String String Int -- Title, Author, Year, Qty | Movie String String String Int -- Title, Director, Year, Qty | CD String String String Int deriving Show -- Title, Artist, Year, Qty I've created an empty list all_Items = [] With the following function I am trying to insert a new book of type Item (Book) into the all_Items addBook all_Items = do putStrLn "Enter the title of the book" tit <- getLine putStrLn "Enter the author of the book" aut <- getLine putStrLn "Enter the year this book was published" yr <- getLine putStrLn "Enter quantity of copies for this item in the inventory" qty <- getLine Book tit aut yr (read qty::Int):all_Items return(all_Items) I however am receiving this error: Couldn't match expected type `IO a0' with actual type `[a1]' The error points to the line where I am using the consing operator to add the new book to the list. I can gather that it is a type error but I can't figure out what it is that I am doing wrong and how to fix it. Thanks in Advance!

    Read the article

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