logo
down
shadow

haskell - will pattern matching work faster for non-specific form of an algebraic data type?


haskell - will pattern matching work faster for non-specific form of an algebraic data type?

Content Index :

haskell - will pattern matching work faster for non-specific form of an algebraic data type?
Tag : haskell , By : chudq7
Date : November 25 2020, 01:01 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

"Pattern matching" of algebraic type data constructors


Tag : haskell , By : Trevor Cortez
Date : March 29 2020, 07:55 AM
help you fix your problem Look at the Data.Data module, the toConstr function in particular. Along with {-# LANGUAGE DeriveDataTypeable #-} that will get you a 1-line solution which works for any type which is an instance of Data.Data. You don't need to figure out all of SYB!
If, for some reason (stuck with Hugs?), that is not an option, then here is a very ugly and very slow hack. It works only if your datatype is Showable (e.g. by using deriving (Show) - which means no function types inside, for example).
constrT :: T -> String
constrT = head . words . show
sameK x y = constrT x == constrT y

Haskell type and pattern matching question: extracting fields from a data type


Tag : haskell , By : druta
Date : March 29 2020, 07:55 AM
This might help you Unfortunately this sort of generic matching on constructors isn't possible directly, but even if it was yours wouldn't work--the extractLispVal function doesn't have a well-defined type, because the type of the result depends on the value of the input. There are various kinds of advanced type-system nonsense that can do things sort of like this, but they're not really something you'd want to use here anyway.
In your case, if you're only interested in extracting particular kinds of values, or if you can convert them to a single type, you can write a function like extractStringsAndAtoms :: LispVal -> Maybe String, for instance.
extractAtom :: LispVal -> String
extractAtom (Atom a) = a
extractAtom :: LispVal -> Maybe String
extractAtom (Atom a) = Just a
extractAtom _ = Nothing
isAtom :: LispVal -> Bool
isAtom = isJust . extractAtom

assumeAtom :: LispVal -> String
assumeAtom x = case extractAtom x of 
                   Just a  -> a
                   Nothing -> error $ "assumeAtom applied to " ++ show x
extractLispVal :: (String -> r) -> ([LispVal] -> r) -> ([LispVal] -> LispVal -> r) 
               -> (Integer -> r) -> (String -> r) -> (Bool -> r) -> (Double -> r)
               -> LispVal -> r
extractLispVal f _ _ _ _ _ _ (Atom x) = f x
extractLispVal _ f _ _ _ _ _ (List xs) = f xs
...
exprToString :: ([String] -> String) -> ([String] -> String -> String) 
             -> LispVal -> String
exprToString f g = extractLispVal id (f . map recur) 
                                  (\xs x -> g (map recur xs) $ recur x)
                                  show show show show
  where recur = exprToString f g
isAtom = extractLispVal (const True) no (const no) no no no no
  where no = const False

Haskell algebraic data type pattern matching


Tag : haskell , By : Cenneca
Date : March 29 2020, 07:55 AM
I wish this help you I have the following: , If you used
concatMap :: (a -> [b]) -> [a] -> [b]
checkAlpha :: Alpha a -> [a]
checkAlpha (Beta a alphas) = a : concatMap checkAlpha alphas

Syntax Error in Pattern Matching on Algebraic Data Type


Tag : haskell , By : Mare Astra
Date : March 29 2020, 07:55 AM
hope this fix your issue You have declared the type BoolLit with constructors T :: BoolLit and F :: BoolLit, and you have also declared the type BExp with constructors BoolLit :: BExp and Or :: BExp -> BExp -> BExp. If you want to wrap the BoolLit type, you'll need to express that in BExp's constructors:
data BExp = BoolLit BoolLit | Or BExpr BExpr
bEval (BoolLit T) = T
instance Eq BoolLit where
    T == T = True
    F == F = True
    _ == _ = False
data BoolLit = T | F deriving (Eq)

Haskell data type pattern matching


Tag : haskell , By : mitry
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Haskell: how to get through 'no instance for'?
  • Haskell Applicative and ErrorT?
  • Haskell: reading multiple command line arguments
  • How do I clear the terminal screen in Haskell?
  • Haskell Input & Passing Values to Functions
  • Whats the difference between IO String and normal String in Haskell
  • What do I learn to "enlighten myself with the ways" of functional programming?
  • Compute all sublists with nth element removed
  • Does Idris have MaybeT?
  • Using the bind function to process a list
  • Giving function type signature inside typeclass instance gives an error
  • How can I use `throwM` with `Except`?
  • How to use atomicModifyIORef with impure functions?
  • Why does the type match on the next line but not on the same line in `do` block?
  • In Haskell apply sqrt in list of tuples
  • How does readIORef work: creates copy or it does not?
  • `f, g, h :: Kleisli ((->) e) a b` <=> `f >>> (g &&& h) = (f >>> g) &&&
  • Natural transformation as an argument in Haskell
  • Haskell: Using multiple let and return a value after the do block inside a function
  • Stack cache builds with various flags
  • What does "<-" mean?
  • Can someone explain to me what is wrong with the logic
  • MonadPlus IO isn't a monoid
  • How to prevent inputs being flushed into output?
  • Understanding the <$> operator
  • Different notions of weak head normal form?
  • Haskell IO action in `StateT a STM b`
  • How can Monoid r => Const r of the optic Fold type be generalized to Contravariant + Applicative?
  • Haskell stack - allow-newer enabled and won't turn off?
  • How to implement a Lens like Getter for a specific type?
  • Why function composition sometimes requires two "." 's to combine two functions
  • Finding equivalent program to proof in typed lambda calculus
  • How to define Linked list on Haskell?
  • How to check the double list length?
  • What are the rules regarding naming in Haskell?
  • How to determine if one can write a total, terminating Haskell function given a type?
  • Instantiate the Endofunctor type with the Show class
  • How to compose functions that return Bools to one function
  • Parse errors when using 'where' notation - how do I format this structure correctly?
  • How to store higher order function data in list in haskell?
  • Why is a typeclass constraint added when I did not define it in my original type definition?
  • Why is there difference between throw and throwIO?
  • Understanding foldr's definition
  • How to use Data.Data?
  • Overload (*) as a -> b -> c
  • Understanding the State Monad
  • Understanding fmap with a monad
  • Writing a flexible "string fetcher"
  • Understanding the filterM function
  • Why is main's return not an exit code?
  • Is main = return () a program?
  • Why can you create a value with "Just (+)"?
  • Haskell naive duplicate filtering
  • Understanding the writer type
  • pattern matching on constants
  • In Haskell, when using the XStrict language extension, is if short-circuiting?
  • Typeclasses and type inference in Haskell
  • Using foldr with only two parameters
  • The simplest way to generically traverse a tree in haskell
  • Is there a better way of writing indexof function?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com