Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell write your version of (.) function

Tags:

haskell

Can someone help me with writing my own version of a (.) function in Haskell? From this post Haskell write your version of a ($) function I know how to determine a type of this function, but I still have the problem with its body. I also do not know why ghci refuses to use the name (..).

 ($$$) :: (b -> c) -> (a -> b) -> a -> c 
 ($$$) f (g x) = ((f g) $) x
 infixr 9 $$$

Another idea of mine was for instance this one:

($$$) :: (b -> c) -> (a -> b) -> a -> c 
($$$) f (g x) = map (f) (g x)
infixr 9 $$$

The error message says that "Parse error in pattern: g".

like image 489
Joanna Avatar asked Aug 04 '26 11:08

Joanna


2 Answers

From the signature:

($$$) :: (b -> c) -> (a -> b) -> a -> c

your function needs 3 arguments. So I would start:

($$$) f g x = ...
      | |  \
      | \   a
      |  \
      |   a->b
    b->c

Update

This attempt at defining ($$$) does not work:

($$$) (f g) x = ...

It says that ($$$) takes two arguments. The way I've started to define ($$$) says that the function takes three arguments.

like image 158
ErikR Avatar answered Aug 06 '26 05:08

ErikR


Are you coming from Lisp? You still seem to assume lists everywhere...

As I already said in the other thread, lists have nothing to do with this task, so neither of (:), foldr or map can possibly be useful here.

More to the point, the occurence of (g x) in the left-hand side of the definition doesn't make sense. (This is not a list, but apparently you think it should be a kind of “argument list”).

As a matter of fact, you could define ($$$) in un-curried form this way:

($$$) :: (b->c) -> (a->b, a) -> c
($$$) f (g, x) = ...

...which is exactly the same thing as the more elegant

f $$$ (g, x) = ...

In this case, you have an argument tuple (g, x), which is more or less equivalent to a Lisp list.

In Haskell, we like to write functions curried though. The signature

($$$) :: (b -> c) -> (a -> b) -> a -> c

is in fact parsed as

($$$) :: (b -> c) -> ( (a -> b) -> (a -> c) )

Hence the way to define such a function is, at the most fundamental level

($$$) = \f -> (\g -> (\x -> ... ))

Which can be written short as

($$$) f g x = ...

or

(f $$$ g) x = ...

In the actual definition part, you should similarly get the grasp of how things are actually parsed. As you have by now figured out, the composition operator can be defined as

($$$) f g x = f(g(x))

In fact, only the outer parentheses are necessary here: the preferred form is

($$$) f g x = f (g x)

or indeed

($$$) f g x = f $ g x

If something like g x or (f g) appears on its own in an expression, it always means that the left function is applied to the right argument. For f g this doesn't make sense, because though f is a function it can not take another function as its argument, only the result of such a function. Well, to get such a result you need to apply g to an argument!

like image 31
leftaroundabout Avatar answered Aug 06 '26 05:08

leftaroundabout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!