haskell

How can this function be written using foldr?

放肆的年华 提交于 2020-01-14 14:07:41
问题 I have this simple function which returns a list of pairs with the adjacents elements of a list. adjacents :: [a] -> [(a,a)] adjacents (x:y:xs) = [(x,y)] ++ adjacents (y:xs) adjacents (x:xs) = [] I'm having problems trying to write adjacents using foldr . I've done some research but nothing seems to give me a hint. How can it be done? 回答1: Tricky folds like this one can often be solved by having the fold build up a function rather than try to build the result directly. adjacents :: [a] -> [(a

error when updating cabal

馋奶兔 提交于 2020-01-14 13:25:08
问题 I cannot update cabal on windows seven. When running cabal install cabal-install , I get the following error: Linking C:\Users\sberg\AppData\Local\Temp\Cabal-1.18.1.2-10956\Cabal-1.18.1.2\dist\setup\setup.exe ... Configuring Cabal-1.18.1.2... setup.exe: does not exist Failed to install Cabal-1.18.1.2 cabal: Error: some packages failed to install: Cabal-1.18.1.2 failed during the configure step. The exception was: ExitFailure 1 cabal-install-1.18.0.2 depends on Cabal-1.18.1.2 which failed to

ApplicativeDo not working with sequencing

两盒软妹~` 提交于 2020-01-14 13:23:48
问题 I have this type, basically a Kleisli arrow: {-# language DeriveFunctor #-} data Plan m i o = Plan (i -> m o) deriving Functor instance (Monad m) => Applicative (Plan m i) where pure x = Plan (\_ -> pure x) Plan f <*> Plan x = Plan (\i -> f i <*> x i) Since it has an Applicative instance, I turn on ApplicativeDo and try to build a value using do-notation: {-# language ApplicativeDo #-} myplan :: Plan IO () () myplan = do pure () pure () It doesn't work: No instance for (Monad (Plan IO ()))

Operations on Church Lists in Haskell

房东的猫 提交于 2020-01-14 12:41:36
问题 I am referring to this question type Churchlist t u = (t->u->u)->u->u In lambda calculus, lists are encoded as following: [] := λc. λn. n [1,2,3] := λc. λn. c 1 (c 2 (c 3 n)) mapChurch :: (t->s) -> (Churchlist t u) -> (Churchlist s u) mapChurch f l = \c n -> l (c.f) n I am thinking about what other list functions I could implement on Churchlists and successfully wrote a conc2 function that concatenates 2 church lists conc2Church l1 l2 c n = l1 c (l2 c n) I also tried a zipWithChurch that

When writing a haskell script, get syntax error near unexpected token

风流意气都作罢 提交于 2020-01-14 12:35:29
问题 When I write a simple script and pass it to runhaskell, it works fine, but not when I add a shebang and try executing it directly. The script is this: #!/usr/local/bin/runhaskell import Data.List (intercalate) main :: IO () main = putStrLn $ intercalate " " $ map show [1..10] If I try $ runhaskell count.hs bash prints 1 2 3 4 5 6 7 8 9 10 as expected, but if I try ./count.hs I get the following error: ./count.hs: line 3: syntax error near unexpected token `(' ./count.hs: line 3: `import Data

When writing a haskell script, get syntax error near unexpected token

时光毁灭记忆、已成空白 提交于 2020-01-14 12:33:54
问题 When I write a simple script and pass it to runhaskell, it works fine, but not when I add a shebang and try executing it directly. The script is this: #!/usr/local/bin/runhaskell import Data.List (intercalate) main :: IO () main = putStrLn $ intercalate " " $ map show [1..10] If I try $ runhaskell count.hs bash prints 1 2 3 4 5 6 7 8 9 10 as expected, but if I try ./count.hs I get the following error: ./count.hs: line 3: syntax error near unexpected token `(' ./count.hs: line 3: `import Data

When writing a haskell script, get syntax error near unexpected token

无人久伴 提交于 2020-01-14 12:33:26
问题 When I write a simple script and pass it to runhaskell, it works fine, but not when I add a shebang and try executing it directly. The script is this: #!/usr/local/bin/runhaskell import Data.List (intercalate) main :: IO () main = putStrLn $ intercalate " " $ map show [1..10] If I try $ runhaskell count.hs bash prints 1 2 3 4 5 6 7 8 9 10 as expected, but if I try ./count.hs I get the following error: ./count.hs: line 3: syntax error near unexpected token `(' ./count.hs: line 3: `import Data

Same Kinds for List of Int compared to List of Int -> Int?

↘锁芯ラ 提交于 2020-01-14 10:29:09
问题 Why do both of these have the same kind? ghci> :k [Int] [Int] :: * ghci> :k [Int -> Int] [Int -> Int] :: * EDIT per @Gabriel Gonzalez's helpful comment. I don't understand kinds well, so I don't have a good basis for expecting the above types to vary in kind . 回答1: Well, let's check. Int :: * [] :: * -> * So when you apply the Int type to the [] type constructor: [] Int :: * Which is just another (legal) way of writing [Int] :: * Ok, that one follows pretty easily. Int :: * (->) :: * -> * ->

Average of large number of Dice Rolls in Haskell

懵懂的女人 提交于 2020-01-14 10:15:54
问题 In an attempt to learn Haskell better, I'm trying to write a program that displays the average value of the sum of 2 die, rolled X number of times. This is fairly simple in C, Java, Python... but I'm stuck in Haskell. Here's a naive attempt: import System.Random main = do g <- getStdGen let trials = 10000000 let rolls = take trials (randomRs (2, 12) g :: [Int]) let average = div (sum rolls) trials print average For low number of trials, the program works. But when I run this code with ten

Cabal 1.18 gives Bus Error 10 on nearly everything on OS X

て烟熏妆下的殇ゞ 提交于 2020-01-14 10:14:28
问题 I upgraded Cabal from the 1.16 version that came with Haskell Platform to 1.18, but nearly every command immediately dies with Bus error: 10 : $ cabal install aeson Bus error: 10 $ cabal sandbox init Bus error: 10 $ cabal list Bus error: 10 ( cabal help still works, though) OS version: OS X Mavericks 10.9.1 Cabal versions: $ cabal --version cabal-install version 1.18.0.2 using version 1.18.1.2 of the Cabal library GHC version: $ ghc --version The Glorious Glasgow Haskell Compilation System,