了解Haskell优先规则如何与多个部分应用程序一起使用 [英] Understanding how Haskell precedence rules work with multiple partial application

查看:64
本文介绍了了解Haskell优先规则如何与多个部分应用程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释Haskell如何确定节的优先级,带有多个参数的函数以及部分应用的函数.有时,我很难弄清当整个表达式带有多个参数时哪个分部函数将应用哪个参数.

Please explain how Haskell determines precedence with sections, functions that take multiple arguments and multiple partially applied functions. Sometimes I find it hard to figure out which partial function will have which argument applied when whole expression takes multiple arguments.

这里有一些示例函数,但是我敢肯定,不同的示例可能更具说明性.第一个摘自带效果的应用程序编程"一文.

Here are some example functions, however I'm sure different examples could be more illustrative. First one is taken from 'Applicative programming with effects' article.

sequence :: [IO a] → IO [a]
sequence [] = return []
sequence (c : cs) = return (:) `ap` c `ap` sequence cs

(.) (.)
(.) (.) (.)

是否存在将此类表达式转换为lambda表达式形式的工具?

Is there a tool to convert such expressions to lambda expression form?

推荐答案

功能应用程序(如a b c d)中的标识符列表被解析为(((a b) c) d).带括号的后缀运算符(.)被视为标识符.

A list of identifiers in a function application like a b c d is parsed as (((a b) c) d). The parenthesized infix operator (.) is treated like an identifier.

所以(.) (.) (.)解析为((.) (.)) (.).

通常,像这样的变量运算符

In general, a variable operator like this

`functionname`

默认情况下,

具有关联性,并且优先级低于函数应用程序,但是可以使用infixlinfixr声明对其进行修改. ap函数的默认值尚未修改,因此sequence的rhs表示为:

by default has left associativity, and lower precedence than function application, but this can be modified with an infixl or infixr declaration. The ap function has not been modified from the default, so the rhs of sequence pases as:

((return (:)) `ap` c) `ap` (sequence cs)

或等效地

ap (ap (return (:)) c) (sequence cs)

这篇关于了解Haskell优先规则如何与多个部分应用程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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