haskell - Weeding duplicates from a list of functions -


is possible remove duplicates (as in nub) list of functions in haskell? basically, possible add instance (eq (integer -> integer))

in ghci:

let fs = [(+2), (*2), (^2)] let cs = concat $ map subsequences $ permutations fs nub cs  <interactive>:31:1: no instance (eq (integer -> integer))   arising use of `nub' possible fix:   add instance declaration (eq (integer -> integer)) in expression: nub cs in equation `it': = nub cs 

thanks in advance.

...

further, based on larsmans' answer, able this

> let fs = [addtwo, double, square] > let css = nub $ concat $ map subsequences $ permutations fs 

in order this

> css  [[],[addtwo],[double],[addtwo,double],[square],[addtwo,square],[double,square],[addtwo,double,square],[double,addtwo],[double,addtwo,square],[square,double],[square,addtwo],[square,double,addtwo],[double,square,addtwo],[square,addtwo,double],[addtwo,square,double]] 

and this

> map (\cs-> call <$> cs <*> [3,4]) css  [[],[5,6],[6,8],[5,6,6,8],[9,16],[5,6,9,16],[6,8,9,16],[5,6,6,8,9,16],[6,8,5,6],[6,8,5,6,9,16],[9,16,6,8],[9,16,5,6],[9,16,6,8,5,6],[6,8,9,16,5,6],[9,16,5,6,6,8],[5,6,9,16,6,8]] 

, original intent.

no, not possible. functions cannot compared equality.

the reason is:

  1. pointer comparison makes little sense haskell functions, since equality of id , \x -> id x change based on whether latter form optimized id.
  2. extensional comparison of functions impossible, since require positive solution halting problem (both functions having same halting behavior necessary requirement equality).

the workaround represent functions data:

data function = addtwo | double | square deriving eq  call addtwo  =  (+2) call double  =  (*2) call square  =  (^2) 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -