haskell

Haskell: bound-values in generic functions

这一生的挚爱 提交于 2019-12-24 10:15:45
问题 i want to do something like: succ' :: (Bounded a, Eq a, Enum a) => a -> a succ' n | n == (maxBound :: a) = minBound :: a | otherwise = succ n but this does not work. how to solve this? 回答1: You don't need the type annotations, and they're the source of the errors you're getting: succ' :: (Bounded a, Eq a, Enum a) => a -> a succ' n | n == maxBound = minBound | otherwise = succ n (This works in Haskell 98 and Haskell 2010, so pretty much any compiler you've got lying around .) Also, I indented

How to force strict evaluation of a sequence of ByteString

≡放荡痞女 提交于 2019-12-24 10:14:46
问题 I have the following Haskell type definition: import Data.Sequence(Seq, length) import Data.ByteString.UTF8(ByteString) type StringSeq = Seq ByteString I have expressions of type StringSeq for which I would like to force strict evaluation with deepseq . So I need to define instances of NFData . I did the following: import Control.DeepSeq(NFData, deepseq) instance NFData ByteString instance NFData a => NFData (Seq a) where rnf s = rnf (length s) So I compute the length of a sequence to force

Haskell program to find the position of an element in a list

你。 提交于 2019-12-24 10:13:38
问题 I need to write a function to find the position of one specific element in a list. i was writing like this: findPos list elt | list == [] = -1 | head list == elt = 0 | otherwise = 1 + (findPos (tail list) elt) but how to do in the case that the element is repeated within the list? For example: list= [2,4,9,4,8] and i want the position of the element "4", then has 2 position : second and fourth. How would be a simply function for that? 回答1: You should return a lazy-evaluated list of indexes

How do I do String replace in Haskell

给你一囗甜甜゛ 提交于 2019-12-24 10:09:32
问题 So I have the following list of tuples and a string type M a = [(String, String)] m = [ ("x", "a car"), ("y", "a animal") ] s = "this is x and y" I am trying to strRep m s => "this is a car and a animal" So this is my attempt so far import Data.List.Utils strRep :: (Show a) => M -> String -> String strRep [] s = s strRep (m:ms) s = replace (fst m) (snd m) s : strRep ms s The above code returns a list of string. I cant quite figure out the proper way to do loop here. 回答1: I'm not sure where

Haskell: how to compare tuples?

北城余情 提交于 2019-12-24 10:00:18
问题 I'm trying to make a tuple list store some information in a particular way. Such as scotland belongs to uk , england belongs to uk , etc. Then take two strings as arguments ( String -> String -> Bool ) to make something like: Main> owns "china" "beijing" True Main> owns "uk" "beijing" False Here's my code: lst = [("uk","scotland"),("uk","england"),("uk","wales"),("china","beijing"),("china","hongkong"),("china","shanghai")] owns :: String -> String -> Bool owns a b = [n|(a,b) <- lst, (n == a)

“No instance for” trait that's already implemented

☆樱花仙子☆ 提交于 2019-12-24 09:59:16
问题 I'd like to use Servant's ClientM monad with finally :: MonadBaseControl IO m => m a -> m b -> m a , but am faced with error No instance for (MonadBaseControl IO ClientM) . Oddly, this exact instance appears to be defined already, be it from an internal module. Do I need to explicitly import such instances somehow? 回答1: It works fine if, as Daniel said, you import the module that defines the desired instance: Prelude> import Control.Exception.Lifted Prelude Control.Exception.Lifted> import

Regex for matching literal strings

安稳与你 提交于 2019-12-24 09:58:50
问题 I'm trying to write a regular expression which will match a string. For simplicity, I'm only concerned with double quote (") strings for the moment. So far I have this: "\"[^\"]*\"" This works for most strings but fails when there is an escaped double quote such as this: "a string \" with an escaped quote" In this case, it only matches up to the escaped quote. I've tried several things to allow an escaped quote but so far I've been unsuccessful, can anyone give me a hand? 回答1: I've managed to

undefined reference to symbol 'pthread_setname_np@@GLIBC_2.12' Haskell Stack error on OpenSuse42.3

混江龙づ霸主 提交于 2019-12-24 09:57:26
问题 I have installed haskell stack and I'm trying to setup ghc for the same. stack version is stack --version Version 1.5.1, Git revision 600c1f01435a10d127938709556c1682ecfd694e (4861 commits) x86_64 hpack-0.17.1 I have updated the ~/.stack/global-project/stack.yaml as below # This is the implicit global project's config file, which is only used when # 'stack' is run outside of a real project. Settings here do _not_ act as # defaults for all projects. To change stack's default settings, edit # '

How do I build stack configuration that allows me to build project with reflex-frp as dependency?

徘徊边缘 提交于 2019-12-24 09:56:30
问题 As in question. I can't get working stack configuration. I want to build project that uses reflex with ghc, preferably 8.0.2 or higher. How do I do it, cause specifying it as an extra deps doesn't seem to work either.. 回答1: Looks like no published version of reflex supports ghc 8.0.2. There are a lot of changes on the develop branch since the 0.4 release: https://github.com/reflex-frp/reflex/compare/cc62c11a6cde31412582758c236919d4bb766ada...develop So first I tried this: resolver: lts-9.5

Pattern matching on types

丶灬走出姿态 提交于 2019-12-24 09:48:58
问题 Is there nice way to write the following "x is of type t" parts? (I suspect I should be using Data.Type.Equality but I'm not sure exactly how) f :: a -> Int g :: b -> Int h :: Typeable t => t -> Maybe Int h x = case x of (x is of type a) -> Just (f x) (x is of type b) -> Just (g x) _ -> Nothing Follow up question 回答1: This is a job for the "type safe cast" bits of Data.Typeable . cast :: (Typeable a, Typeable b) => a -> Maybe b pulls the runtime type information out of the Typeable