4

Hey haskellers and haskellettes, is it possible to load a module functions in a list. in my concrete case i have a list of functions all checked with or

checkRules :: [Nucleotide] -> Bool
checkRules nucs = or $ map ($ nucs) [checkRule1, checkRule2]

i do import checkRule1 and checkRule2 from a seperate module - i don't know if i will need more of them in the future.

i'd like to have the same functionality look something like

-- import all functions from Rules as rules where
-- :t rules ~~> [([Nucleotide] -> Bool)]

checkRules :: [Nucleotide] -> Bool
checkRules nucs = or $ map ($ nucs) rules

the program sorts Pseudo Nucleotide Sequences in viable and nonviable squences according to given rules. thanks in advance ε/2


Addendum: So do i think right - i need:

genList :: File -> TypeSignature -> [TypeSignature]
chckfun :: (a->b) -> TypeSignature -> Bool

at compile time. but i can't generate a list of all functions in the module - as they most probably will have not the same type signature and hence not all fit in one list. so i cannot filter given list with chckfun.

  • In order to do this i either want to check the written type signatures in the source file (?) or the inferenced types given by the compiler(?).
  • another problem that comes to my mind is: not every function written in the source file might get exported ?

  • Is this a problem a haskell beginner should try to solve after 5 months of learning - my brain is shaped like a klein's bottle after all this "compile time thinking".

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74

1 Answers1

6

There is a nice package on Hackage just for this: language-haskell-extract. In particular, the Template Haskell function functionExtractor takes a regular expression and returns a list of the matching top level bindings as (name, value) pairs. As long as they all have matching types, you're good to go.

{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.Extract

myFoo = "Hello"
myBar = "World"

allMyStuff = $(functionExtractor "^my")

main = print allMyStuff

Output:

[("myFoo", "Hello"), ("myBar", "World")]
hammar
  • 138,522
  • 17
  • 304
  • 385