haskell

wraps [] around each top-level element of list

喜你入骨 提交于 2019-12-24 09:28:26
问题 wrap [1,2,3] should output [[1],[2],[3]] wrap [[1],[2],[3]] should output [ [[1]], [[2]], [[3]] ] my implementation is: wrap [] = [] wrap (x:xs) = [x] : [wrap xs] and haskell output an error: Occurs check: cannot construct the infinite type: t ~ [t] Expected type: [t] -> [t] Actual type: [t] -> [[t]] 回答1: [wrap xs] wraps the entire result of wrap xs . You don't want to wrap the entire result. You only want to wrap each individual element of the remainder of the list and that is exactly what

Pattern Matching a Haskell list of variable size

血红的双手。 提交于 2019-12-24 09:17:28
问题 EDIT : I shouldn't be coding when this tired. I was compiling a different copy of the program than I was running. Sorry for wasting your time. I have the following code to make a single argument optional in my program's startup. main = do args <- getArgs handleArgs args handleArgs :: [String] -> IO () handleArgs (server:nick:channel:[]) = IRC.startIRC server 6667 nick channel handleArgs (server:port:nick:channel:[]) = IRC.startIRC server (read port :: Int) nick channel handleArgs _ = putStrLn

How to use “IO String” as an HTTP response in Happstack?

有些话、适合烂在心里 提交于 2019-12-24 09:14:13
问题 I'm retrieving data from a database using HDBC, then trying to send this data to a web client using Happstack. myFunc :: Integer -> IO String myFunc = ... fetch from db here ... handlers :: ServerPart Response handlers = do decodeBody (defaultBodyPolicy "/tmp/" 0 1000 1000) msum [ dir "getData" $ ok $ toResponse $ myFunc $ toInteger 1 ] mainFunc = simpleHTTP nullConf handlers When I build the above code I get this error: No instance for (ToMessage (IO String)) arising from a use of

ByteString representation of a Double conversion

偶尔善良 提交于 2019-12-24 09:12:35
问题 I'm writing my own WAVE parser that uses Conduit so I can stream values, one by one, through a pipeline. I get a sample from a .wav file via hGet (n is the number of bytes per sample for that wav file): bytes <- hGet h n This gives me a ByteString with a representation of the Double value of the sample. E.g.: "\131\237\242" represents -0.10212671756744385 "g\238\242" represents -0.10209953784942627 "\215\238\242" represents -0.10208618640899658 . The possible values are between -1 and +1. Is

Model a serial format in the type system, like Servant

不打扰是莪最后的温柔 提交于 2019-12-24 08:59:55
问题 I'm working on an API integration that ignores the existence of XML or JSON in favor of just appending character data. (The Metro2 format, if interested) I'm simplifying, but imagine that a person needs to be serialized like this: At pos 0, 4 chars: Number of bytes in the message At pos 5: 6 chars: "PERSON" hard coded At pos 11: 20 chars: Name, left-aligned and space-padded At pos 21: 8 chars: Birthday, YYYYMMDD At pos 29: 3 chars: Age, right-aligned and zero-padded Numeric fields are always

How does outermost evaluation work on an application of a curried function?

只愿长相守 提交于 2019-12-24 08:58:34
问题 mult is defined as a curried function: mult :: Int -> Int -> Int mult x = \y -> x * y In mult (1+2) (2+3) , what are the redex's. and are they mult(1+2) , 1+2 and 2+3 ? What is the outermost redex, and is it 2+3 ? Innermost evaluation works on the expression as following, according to Programming in Haskell by Hutton: mult (1+2) (2+3) = { applying the first + } mult 3 (2+3) = { applying mult } (\y -> 3 * y) (2+3) = { applying + } (\y -> 3 * y) 5 = { applying the lambda } 3 * 5 = { applying *

Haskell: Replace mapM in a monad transformer stack to achieve lazy evaluation (no space leaks)

余生长醉 提交于 2019-12-24 08:58:26
问题 It has already been discussed that mapM is inherently not lazy, e.g. here and here. Now I'm struggling with a variation of this problem where the mapM in question is deep inside a monad transformer stack. Here's a function taken from a concrete, working (but space-leaking) example using LevelDB that I put on gist.github.com: -- read keys [1..n] from db at DirName and check that the values are correct doRead :: FilePath -> Int -> IO () doRead dirName n = do success <- runResourceT $ do db <-

Understanding cabal dependency messages

五迷三道 提交于 2019-12-24 08:56:47
问题 currently when I try to install GOA I get the following message : >sudo cabal install goa Resolving dependencies... In order, the following would be installed: directory-1.1.0.2 (reinstall) changes: filepath-1.3.0.0 -> 1.2.0.1 process-1.0.1.5 (new version) goa-3.1 (new package) cabal: The following packages are likely to be broken by the reinstalls: .... My question is whether this means that I currently have "filepath-1.3.0.0" as default, and cabal (upon a --force-reinstalls) is going to

Haskell: confused about uses of Num typeclass

杀马特。学长 韩版系。学妹 提交于 2019-12-24 08:29:05
问题 I'm confused about why this works: f :: Num a => a -> a f x = x * 42 But this doesn't: g :: Num a => a -> a g x = x * 4.2 I had understood that Num enclosed all types that implemented operators (+) , (-) , (*) . So, if 42 is an Int and 4.2 is a Fractional , and both types implement the operator (*) , why am I getting this error for g ?: Could not deduce (Fractional a) arising from the literal ‘4.2’ from the context (Num a) bound by the type signature for g :: Num a => a -> a at file.hs:9:6-20

I want to add another condition which removes a user that has already liked a film

╄→尐↘猪︶ㄣ 提交于 2019-12-24 08:27:33
问题 I am trying to create a function which allows a user to dislike a film. However the user can only exist in the list of likes/dislikes but not both addUserDislikes :: Title -> User -> [Film] -> [Film] addUserDislikes title user db = [ if ti == title && elem user dislike == False then (ti, di, yr,like,dislike++ [user])else (ti, di, yr, like, dislike) | (ti, di, yr, like, dislike) <- db] This function only adds the user to the list of Dislikes but does not remove it from list of likes how do I