-3

I have two functions:

f1 :: Bool -> Int
f1 x 
    | x == True     = 5
    | x == False    = 10

f2 :: Int -> Int
f2 x = x * 2

Since the output of f1 is the correct input for f2, how come

f2 f1 True

causes an error?

I didnt know what to tag so the tags might be inappropriate.

Arthur
  • 347
  • 1
  • 4
  • 14
  • Try `f2 (f1 True)` or `f2 $ f1 True`. – jub0bs Dec 21 '14 at 21:36
  • @Jubobs Could you give me an example where (f g) x is a well-formed expression, with f and g both being functions? – Arthur Dec 21 '14 at 21:50
  • @Arthur `filter odd [1, 2, 3]` for example. – sepp2k Dec 21 '14 at 21:55
  • @Arthur Consider `doTwice f x = f (f x)` It has the type `(a->a)->a->a`. Then `(doTwice (\x -> x+1)) 3` is well formed. – Cirdec Dec 21 '14 at 22:01
  • @Cirdec Thanks, very helpful. What about and example where f . g x is a well formed expression? – Arthur Dec 21 '14 at 22:04
  • `let f = head; g = filter; x = odd; in map (f . g x) [[1,2,3],[4,5,6]]` – amalloy Dec 21 '14 at 22:07
  • 2
    @Arthur Now I'm convinced those are both homework questions. Here's a hint. If `(f g) x` is well formed, there must exist some function `h` such that `h . f g` is well formed. – Cirdec Dec 21 '14 at 22:35

1 Answers1

4

Function application is left-associative, so f2 f1 True is the same as (f2 f1) True, not f2 (f1 True). (f2 f1) True is clearly mis-typed as the argument of f2 has type Int, but f1 has type Bool -> Int, not Int.

sepp2k
  • 363,768
  • 54
  • 674
  • 675