haskell

Monad and MonadIO for custom type

♀尐吖头ヾ 提交于 2020-01-06 14:16:05
问题 I have a Logger type of kind * -> * which can take any type and log the value in a file. I am trying to implement this in a monadic way so that I log and keep working the same. My code looks like import Control.Applicative import Control.Monad import System.IO import Control.Monad.IO.Class instance Functor Logger where fmap = liftM instance Applicative Logger where pure = return (<*>) = ap newtype Logger a = Logger a deriving (Show) instance Monad (Logger) where return = Logger Logger logStr

Haskell http-conduit-1.9.6 “No instance for (Read UTCTime)” compilation error

笑着哭i 提交于 2020-01-06 13:58:25
问题 I'm trying to install http-conduit-1.9.6 (my Haskell application needs that version), and my "cabal install http-conduit-1.9.6" gives me the following error: Building http-conduit-1.9.6... Preprocessing library http-conduit-1.9.6... [ 1 of 12] Compiling Network.HTTP.Conduit.Util ( Network/HTTP/Conduit/Util.hs, dist/build/Network/HTTP/Conduit/Util.o ) [ 2 of 12] Compiling Network.HTTP.Conduit.ConnInfo ( Network/HTTP/Conduit/ConnInfo.hs, dist/build/Network/HTTP/Conduit/ConnInfo.o ) [ 3 of 12]

Simple haskell code can't compile

℡╲_俬逩灬. 提交于 2020-01-06 12:52:27
问题 I start learning Haskell, and just tried a simple code, and it shows me some errors when I run doubleMe :: Int -> Int doubleMe x = x + x main = do doubleMe 2 ghc -c first.hs The errors are: $ ghc -c first.hs first.hs:4:1: Couldn't match expected type IO t0' with actual type Int' In the expression: main When checking the type of the function `main' While I use GCHi to debug, there were no issues to load doubleMe function first, and call it later. Any help would be appreciated. 回答1: Every

Remove elements at positions n and n-1 in a Haskell list, when n fits a predicate

微笑、不失礼 提交于 2020-01-06 12:44:27
问题 Say I have a list of all the integers from 2 to 20 . [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] I also have a function f x that returns either True or False . When I apply this function to the element at position n and it equals to True , I want to remove it and its precedent element (which is the element at position n-1 ). I want to keep doing this until the list is empty of elements for which the function equals to True , as well as their preceding elements. Example: Let's say that

Remove elements at positions n and n-1 in a Haskell list, when n fits a predicate

让人想犯罪 __ 提交于 2020-01-06 12:44:07
问题 Say I have a list of all the integers from 2 to 20 . [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] I also have a function f x that returns either True or False . When I apply this function to the element at position n and it equals to True , I want to remove it and its precedent element (which is the element at position n-1 ). I want to keep doing this until the list is empty of elements for which the function equals to True , as well as their preceding elements. Example: Let's say that

Decimal and binary number handling

徘徊边缘 提交于 2020-01-06 09:00:06
问题 I'm working on a code which capable of converting decimal to a binary value. Comparing to other languages haskell needs few lines of code to bring this up. Below is the code i worked out. binaryToDec :: [Int] -> Int binaryToDec [] = [] binaryToDec (x:xs) = (+) (x * 2 ^(length xs)) this code gives an error which i tried so hard to figure out. but i couldn't. Assignment.hs:61:18: Couldn't match expected type `Int' with actual type `[a0]' In the expression: [] In an equation for `binaryToDec':

How to delete an element from a Leafy Binary Tree (Haskell)

最后都变了- 提交于 2020-01-06 05:51:19
问题 So, this tree is NOT a Binary Search Tree. It is in no particular order, and is just in this order for quick access to specific indices (nth element), rather than whether an element exists or not. The form of the Tree is like so: data Tree a = Leaf a | Node Int (Tree a) (Tree a) deriving Show For this specific tree, the "Int" from the Node constructor is the number of elements underneath that node (or number of leaves). Using this structure, I copied parts of the Tree functions available in a

Invoking nested functions in Haskell

 ̄綄美尐妖づ 提交于 2020-01-06 05:47:09
问题 I have defined a function called initials inside a function called person , but I can't figure out how to call initials outside of person : main = --I attempted to print the output of the initials function here. (putStrLn ((person "firstName" "lastName") . initials)) --Not in scope: `initials' --If this function call worked correctly, the output would be "f.l.". person firstName lastName = firstName ++ ["."] ++ lastName where fullName = firstName ++ " " ++ lastName firstInitial = firstName !!

Keep record of previously visited states when searching

余生长醉 提交于 2020-01-06 05:24:10
问题 I am programming several search functions, for which I use the Node datatype: data Node a = Node { [...] , getPath :: [a] -- ^ Previous states this node has visited } That field, getPath , is what I use to check if I have previously visited that state in that node: when expanding a new node, I check that by doing: visited = `elem` path It works, but it becomes incredibly costly when there are a lot of nodes expanded and the paths become too long. Is there a better way to keep track of the

Access violation in GHCI

不问归期 提交于 2020-01-06 04:37:14
问题 I have a Haskell program using the FFI to import C++ functions. I'm using Windows. When I compile to an executable or to a DLL, this works. But when I load a module in GHCI, this doesn't always work: sometimes this works, sometimes I get Access violation in generated code when reading 0000000000000000 whenever I call a function from the module (even if this function has nothing to do with the C++). Do you know how to prevent that? 来源: https://stackoverflow.com/questions/47849032/access