1

Hello i would like to print the names of functions in a list. Is it somehow possible to do so?

my example:

import Text.Show.Functions
f1 x = if x == 1 then 1 else 0
f2 x = if x == 2 then 1 else 0
f3 x = if x == 1 then 2 else 0
fs = [f1,f2,f3]
fs

writes out [<function>,<function>,<function>] but I want [f1,f2,f3]

1
  • Functions don't have names per se; you can bind one or more names to a single function, but the function itself has no notion of which names are bound to it. Commented Mar 17, 2018 at 15:09

1 Answer 1

4

No, it is not possible. If you want named functions, you will have to whip them up yourself; e.g.

data Named a = Named { name :: String, val :: a }
instance Show (Named a) where show = name
fs = [Named "f1" f1, Named "f2" f2, Named "f3" f3]
Sign up to request clarification or add additional context in comments.

1 Comment

It might go without saying, but now fs has a type like [Named (a -> b)], not [(a -> b)]. Subsequent code using fs would need to adapt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.