I try to compose two function with type specifying.
foo :: Num a => a -> a
foo a = a + 2
bar :: Num a => a -> a
bar a = a * 2
fooBarCompose :: (Num a, Num b, Num c) => (a -> b) -> (c -> a) -> c -> b
fooBarCompose f g = f . g
My module compiles, but in runtime when I invoke
fooBarCompose bar foo
I get an error:
No instance for (Show (b0 -> b0))
(maybe you haven't applied enough arguments to a function?)
arising from a use of ‘print’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
Any ideas why I get this?
Any ideas why I get this?
You don't . Everything you've written works just fine. You can use fooBarCompose bar foo
in any program you want.
Only, if you try to evaluate it in GHCi, it has a problem: fooBarCompose bar foo
is a function. How the heck is it supposed to show a function ? Display an exhaustive list of all possible inputs and corresponding results? Clearly not feasible. GHCi uses print
under the hood, which simply invokes show
. And, well, because it's not possible to show a function, it gives you an error message saying exactly this.
OTOH, the result of applying a function to any single value can easily be shown, eg
> fooBarCompose bar foo 2 -- aka `bar . foo $ 2`
8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.