Hi. I try process file which writen by russian symbols. When read and after write text to file i get something like:
"\160\192\231\229\240\225\224\233\228\230\224\237"
How i can get normal symbols ?
Thanks
Hello to all, i wonder can a IO() function return tuple because i would like to get these out of this function as input for another function.
investinput :: IO()->([Char], Int)
investinput = do
putStrLn "Enter Username : "
username <- getLine
putStrLn "Enter Invest Amount : "
tempamount <- getLIne
let amount = show tempamount
return (username, amount)
Please help.
Thanks.
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.
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!
I know you can convert a String to an number with read like so:
Prelude read "3" :: Int
3
Prelude read "3" :: Double
3.0
But how do you grab the string representation of an Int value?
It sounds silly, but I can't get it. Why can the expression [] == [] be typed at all? More specifically, which type (in class Eq) is inferred to the type of list elements?
In a ghci session, I see the following:
Prelude> :t (==[])
(==[]) :: (Eq [a]) => [a] -> Bool
But the constraint Eq [a] implies Eq a also, as is shown here:
Prelude> (==[]) ([]::[IO ()])
<interactive>:1:1:
No instance for (Eq (IO ()))
arising from use of `==' at <interactive>:1:1-2
Probable fix: add an instance declaration for (Eq (IO ()))
In the definition of `it': it = (== []) ([] :: [IO ()])
Thus, in []==[], the type checker must assume that the list element is some type a that is in class Eq. But which one? The type of [] is just [a], and this is certainly more general than Eq a = [a].
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?
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!
Hello.
Whenever I close OpenGL window it makes ghci console from which the app was started to immediately disappear. Is there a way to prevent this behaviour? A sample app.
Thanks.
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
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
I have the following code:
foo :: Int -> [String] -> [(FilePath, Integer)] -> IO Int
foo _ [] _ = return 4
foo _ _ [] = return 5
foo n nameREs pretendentFilesWithSizes = do
result <- (bar n (head nameREs) pretendentFilesWithSizes)
if result == 0
then return 0 -- <========================================== here is the error
else foo n (tail nameREs) pretendentFilesWithSizes
I get an error on the line with the comment above, the error is:
aaa.hs:56:2:
parse error (possibly incorrect indentation)
I'm working with emacs, there's no spaces, and i do not understand what did i do wrong.
I am getting a Couldn't match expected type error on this code and am not sure why. Would appreciate it if someone could point me in the right direction as to fixing it.
import qualified Data.ByteString.Lazy as S
import Data.Binary.Get
import Data.Word
getBinary :: Get Word16
getBinary = do
a <- getWord16be "Test.class"
return (a)
main :: IO ()
main = do
contents <- S.getContents
print getBinary contents
Specifically it cannot match expected type 'S.ByteString - IO ()' to inferred type 'IO ()'
I am trying to parse an input stream where the first line tells me how many lines of data there are. I'm ending up with the following code, and it works, but I think there is a better way. Is there?
main = do
numCases <- getLine
proc $ read numCases
proc :: Integer -> IO ()
proc numCases
| numCases == 0 = return ()
| otherwise = do
str <- getLine
putStrLn $ findNextPalin str
proc (numCases - 1)
Note: The code solves the Sphere problem https://www.spoj.pl/problems/PALIN/ but I didn't think posting the rest of the code would impact the discussion of what to do here.
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?
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
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 )?
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
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?
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?
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?
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?
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!
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!!