Haskell函数组合,(。)(。)的类型以及它如何呈现 [英] Haskell function composition, type of (.)(.) and how it's presented

查看:94
本文介绍了Haskell函数组合,(。)(。)的类型以及它如何呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道:

$ p $ (。)=(fg)x = f(gx)

它的类型是(B-> C) - >(A-> B) - > A-> C
但是怎么样:

 (。)(。)= _? = _? 

这是如何表示的?我想到:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ))//这个
(。)(。)=(fgh)x = f(g(hx))//或这个

但是据我试图得到它的类型,GHCi告诉我什么是不正确的。
那么_?是什么?



另外 - 函数/运算符 $ 做什么?

解决方案

首先,你对于你的记法嗤之以鼻。 code>(。)=(fg)x = f(gx) - 这不是真的

什么是真的:

 (。)fgx =(fg)x = f(gx)
(。)= \fgx - > f(gx)

其类型由

 (。)::(b  - > c) - > (a  - > b) - > a  - > c 
- n.b.小写,因为它们是类型*变量*

同时
$ (。)(。)::(a - > b - > d) - > a - > (c→b)→> c - > d
- 我重命名了变量ghci给了我

现在让我们来看看

($'$'$'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ \\ fgx - > f(gx))
= \ g'x' - > (g x - > f(g x))(g'x')
= \ g'x' - > \ g x - > (g'x')(g x)
= \ f y - > \ g x - > (f y)(g x)
= \ f y g x - > f y(g x)
= \ f y g x - > (f y。g)x
= \ f y g - > f y。 g






<和> ($ )

 ($)::(a  - > b) - > a  - > b 
f $ x = fx

($)就是函数应用程序。但是通过并置的函数应用具有较高的优先级,通过($)的函数应用程序的优先级较低。

  square $ 1 + 2 * 3 = square(1 + 2 * 3)
square 1 + 2 * 3 =(square 1)+ 2 * 3 - 这些行不同


So i know that:

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

And it's type is (B->C)->(A->B)->A->C But what about:

(.)(.) = _? = _?

How this is represented? I thought of:

(.)(.) = (f.g)(f.g)x = f(g(f(g x))) // this
(.)(.) = (f.g.h)x = f(g(h x)) // or this

But as far as i tried to get type of it, it's not correct to what GHCi tells me. So what are both "_?"

Also - what does function/operator $ do?

解决方案

First off, you're being sloppy with your notation.

(.) = (f.g) x = f (g x)  -- this isn't true

What is true:

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

And its type is given by

(.) :: (b -> c) -> (a -> b) -> a -> c
       -- n.b. lower case, because they're type *variables*

Meanwhile

(.)(.) :: (a -> b -> d) -> a -> (c -> b) -> c -> d
          -- I renamed the variables ghci gave me

Now let's work out

(.)(.) = (\f' g' x' -> f' (g' x')) (\f g x -> f (g x))
       = \g' x' -> (\f g x -> f (g x)) (g' x')
       = \g' x' -> \g x -> (g' x') (g x)
       = \f y -> \g x -> (f y) (g x)
       = \f y g x -> f y (g x)
       = \f y g x -> (f y . g) x
       = \f y g -> f y . g


And ($)?

($) :: (a -> b) -> a -> b
f $ x = f x

($) is just function application. But whereas function application via juxtaposition is high precedence, function application via ($) is low precedence.

square $ 1 + 2 * 3 = square (1 + 2 * 3)
square 1 + 2 * 3 = (square 1) + 2 * 3  -- these lines are different

这篇关于Haskell函数组合,(。)(。)的类型以及它如何呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆