pattern-synonyms

How can I write this pattern synonym without ambiguous type errors?

南笙酒味 提交于 2020-01-13 10:25:17
问题 Using ViewPatterns and Data.Typeable , I’ve managed to write a function that allows me to write something resembling case analysis on types. Observe: {-# LANGUAGE GADTs, PatternSynonyms, RankNTypes, ScopedTypeVariables , TypeApplications, TypeOperators, ViewPatterns #-} import Data.Typeable viewEqT :: forall b a. (Typeable a, Typeable b) => a -> Maybe ((a :~: b), b) viewEqT x = case eqT @a @b of Just Refl -> Just (Refl, x) Nothing -> Nothing evilId :: Typeable a => a -> a evilId (viewEqT @Int

Haskell Bytestrings: How to pattern match?

怎甘沉沦 提交于 2019-12-30 00:33:06
问题 I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x

Haskell Bytestrings: How to pattern match?

荒凉一梦 提交于 2019-12-30 00:33:03
问题 I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x

Constants in Haskell and pattern matching [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-23 15:58:01
问题 This question already has answers here : Haskell - Using a constant in pattern matching (2 answers) Closed 3 years ago . How is it possible to define a macro constant in Haskell? Especially, I would like the following snippet to run without the second pattern match to be overlapped. someconstant :: Int someconstant = 3 f :: Int -> IO () f someconstant = putStrLn "Arg is 3" f _ = putStrLn "Arg is not 3" 回答1: You can define a pattern synonym: {-# LANGUAGE PatternSynonyms #-} pattern

Haskell - Using a constant in pattern matching

删除回忆录丶 提交于 2019-12-19 09:38:41
问题 Let's say I have the following code (text in <> is a shorthand, not actually part of the code): data A = <something> defaultA :: A defaultA = <Really complicated expression of type A> Now I want to have a function pattern match on defaultA , like this: f defaultA = <case 1> f _ = <case 2> However, defaultA in the first line becomes a new variable, not a condition that means the parameter will equal defaultA . The best way I know to achieve something like what I want is: f x | x == defaultA =

extracting synonyms using wordnet

让人想犯罪 __ 提交于 2019-12-11 17:05:40
问题 I am currently working on my thesis and implementing the solution in R language. i have to find synonyms using word-net dictionary library. i get the synonyms against single word but when i try to get synonyms using loop for set of words i get the error "Subscription is out of bond".. kindly if some one can guide me how to get synonyms for against each word in text using loop or is there any other way to do it? here is the code i am trying *my_corpus <- "closure animal wrong carnivore

Pattern matching on a private data constructor

落爺英雄遲暮 提交于 2019-12-09 18:03:26
问题 I'm writing a simple ADT for grid axis. In my application grid may be either regular (with constant step between coordinates), or irregular (otherwise). Of course, the regular grid is just a special case of irregular one, but it may worth to differentiate between them in some situations (for example, to perform some optimizations). So, I declare my ADT as the following: data GridAxis = RegularAxis (Float, Float) Float -- (min, max) delta | IrregularAxis [Float] -- [xs] But I don't want user

Pattern matching on a private data constructor

左心房为你撑大大i 提交于 2019-12-04 04:06:02
I'm writing a simple ADT for grid axis. In my application grid may be either regular (with constant step between coordinates), or irregular (otherwise). Of course, the regular grid is just a special case of irregular one, but it may worth to differentiate between them in some situations (for example, to perform some optimizations). So, I declare my ADT as the following: data GridAxis = RegularAxis (Float, Float) Float -- (min, max) delta | IrregularAxis [Float] -- [xs] But I don't want user to create malformed axes with max < min or with unordered xs list. So, I add "smarter" construction

Haskell Bytestrings: How to pattern match?

主宰稳场 提交于 2019-11-30 03:21:38
I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x:[]) = [x] <...> yields Couldn't match expected type `BS.ByteString' against inferred type `[a]' In the

Haskell constructor aliases

陌路散爱 提交于 2019-11-26 21:56:11
问题 Is there a way to have something equivalent to creating "constructor aliases" in Haskell? I'm thinking similar to type aliases where you can give the type a different name but it still behaves in every way as the aliased type. My use case is a system where I have an assigned time as a property of some objects I'm modelling, so UTCTime . Some of these could be "variable" times, meaning it might not yet be assigned a time or the time it does have is "movable". So Maybe UTCTime . But only some