haskell

Tree building function in Haskell (Homework)

倖福魔咒の 提交于 2020-01-02 22:06:11
问题 data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Eq, Show) unfoldTree:: (b -> Maybe (b, a, b)) -> b -> Tree a unfoldTree f b = case f b of Nothing -> Leaf Just (lt, x, rt) -> Node (unfoldTree f lt) x (unfoldTree f rt) Given the two piece of information above, I'm asked to implement a tree building function. and my attempt is treeBuild :: Integer -> Tree Integer treeBuild 0 = Leaf treeBuild n = treeUnfold (\b -> if b < 2^n-1 then Just(2*b, b + 1, 2*b + 1) else Nothing) 0 The base case

heap usage for CPS vs non-CPS parsers in Haskell's parsec

試著忘記壹切 提交于 2020-01-02 13:56:25
问题 I'm trying to write the following parser using parsec: manyLength :: forall s u m a. Monad m => ParsecT s u m a -> ParsecT s u m Int manyLength p = go 0 where go :: Int -> ParsecT s u m Int go !i = (p *> go (i + 1)) <|> pure i This is like the many function, but instead of returning [a] , it returns the number of times Parser a succeeds. This works, but I can't seem to make it run in constant heap space. This makes sense, since the recursive call to go is not in the tail-call position. If

Get Column in Haskell CSV and infer the column type

左心房为你撑大大i 提交于 2020-01-02 13:33:32
问题 I'm exploring a csv file in an interactive ghci session (in a jupyter notebook): import Text.CSV import Data.List import Data.Maybe dat <- parseCSVFromFile "/home/user/data.csv" headers = head dat records = tail dat -- define a way to get a particular row by index indexRow :: [[Field]] -> Int -> [Field] indexRow csv index = csv !! index indexRow records 1 -- this works! -- Now, define a way to get a particular column by index indexField :: [[Field]] -> Int -> [Field] indexField records index

Composing Monadic Functions with `<=<`

耗尽温柔 提交于 2020-01-02 13:25:20
问题 I'm trying to understand the <=< function: ghci> :t (<=<) (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c As I understand it, I give it 2 functions and an a , and then I'll get an m c . So, why doesn't this example compile? import Control.Monad f :: a -> Maybe a f = \x -> Just x g :: a -> [a] g = \x -> [x] foo :: Monad m => a -> m c foo x = f <=< g x For foo 3 , I would expect Just 3 as a result. But I get this error: File.hs:10:15: Couldn't match expected type `a0 -> Maybe c0' with

Can I mock an interactive program using the state monad?

时光毁灭记忆、已成空白 提交于 2020-01-02 10:18:35
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

Can I mock an interactive program using the state monad?

倖福魔咒の 提交于 2020-01-02 10:18:17
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

Can I mock an interactive program using the state monad?

大兔子大兔子 提交于 2020-01-02 10:18:08
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

Why negative number in the following if condition throws error? [duplicate]

穿精又带淫゛_ 提交于 2020-01-02 10:04:06
问题 This question already has an answer here : Type error when testing a function with a negative number (1 answer) Closed 2 years ago . I am watching 'Haskell Fundamentals Part 1' in Pluralsight. In the second chapter, the author shows an very simple function with if in it. When I tried it, I am getting error everytime when I tried the function with negative number. Here is the function posOrNeg x = if x >= 0 then "Positive" else "Negative" When I tried the method with positive number it worked

Determining a total, terminating function

二次信任 提交于 2020-01-02 09:57:42
问题 I am trying to determine whether for the following type: ((a -> c) -> c) -> a a total, terminating function can be written using that type as a function signature? I understand that in order for a function to be total it has to be defined for all possible values of its input. However, I am not sure what exactly does it means for a function to be terminating. For a function to be terminating, does it have to return a value (and not go into an infinite loop for example)? Further, what methods

How to submit html form with Haskell

痴心易碎 提交于 2020-01-02 09:57:12
问题 I know how to use http-conduit package's simplehttp to retrieve a page from an URL. Now what if on that web page there is an input text field and a submit button. Can I also use http-conduit to fill that text field and push the button and retrieve the resulting page ? 回答1: Yes, you can use either urlEncodedBody or, for multipart messages, the MultipartFormData module. 回答2: According to Snoyman, it seems you can do it with http-conduit . But you may want to look on Sphider package which is