标签: infix-notation

':..'在Haskell中意味着什么?

我正在阅读以下数据类型:

data Ne
  = NVar Id
  | Ne :.. (Clos Term)
  | NSplit Ne (Bind (Bind (Clos Term)))
  | NCase Ne (Clos [(Label, Term)])
  | NForce Ne
  | NUnfold Ne (Bind (Clos Term))
  deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)

什么是:..在第二个成员声明中?

constructor haskell infix-notation

19
推荐指数
2
解决办法
840
查看次数

使用C语言计算中缀表达式的最简单方法是什么?

假设用户输入中缀表达式作为字符串?使用C语言评估表达式结果的最简单方法(最简单的意思是短语 t)是什么?

可能的方法是将其转换为后缀然后使用stacks.But它相当长的过程.有没有办法使用像atoi()eval()这样的函数来简化工作?

c math expression infix-notation evaluate

16
推荐指数
3
解决办法
6070
查看次数

何时在Scala中缀表示法中使用括号

在Scala中编程时,我会做越来越多的功能.但是,使用中缀表示法时,很难判断何时需要括号,何时不需要.

例如下面的代码:

def caesar(k:Int)(c:Char) = c match {
    case c if c isLower => ('a'+((c-'a'+k)%26)).toChar
    case c if c isUpper => ('A'+((c-'A'+k)%26)).toChar
    case _ => c
}

def encrypt(file:String,k:Int) = (fromFile(file) mkString) map caesar(k)_
Run Code Online (Sandbox Code Playgroud)

(fromFile(file)mkString)需要括号才能编译.删除后,我收到以下错误:

Caesar.scala:24: error: not found: value map
    def encrypt(file:String,k:Int) = fromFile(file) mkString map caesar(k)_
                                                                 ^
one error found
Run Code Online (Sandbox Code Playgroud)

mkString显然返回一个字符串,在其上(通过隐式转换AFAIK)我可以使用map函数.

为什么这个特殊情况需要括号?关于何时以及为何需要它,是否有一般指导原则?

dictionary functional-programming scala infix-notation parentheses

15
推荐指数
2
解决办法
3882
查看次数

"中缀"如何运作?

我在玩同infixr,infixlinfix声明.我知道如何infixrinfixl工作原理:

-- Test expression: 40 +++ 20 +++ 50 +++ 10 * 10

-- infixr 8 +++ -- Calculated as: (40 +++ (20 +++ (50 +++ 10))) * 10. Result: 630.
-- infixl 8 +++ -- Calculated as: (((40 +++ 20) +++ 50) +++ 10) * 10. Result: 800.

-- infixr 6 +++ -- Calculated as: 40 +++ (20 +++ (50 +++ (10 * 10))). Result: 75.
-- infixl 6 …
Run Code Online (Sandbox Code Playgroud)

haskell infix-notation

14
推荐指数
2
解决办法
7127
查看次数

为什么f <$> g <$> x等价于(f.g)<$> x虽然<$>不是右关联的?

为什么f <$> g <$> x相当于(f . g) <$> x虽然<$>不是正确联想的?

(这种等价在普通的流行习语中是有效的$,但目前$是正确联想的!)

<*>具有相同的关联性和优先级<$>,但行为不同!

例:

Prelude Control.Applicative> (show . show) <$> Just 3
Just "\"3\""
Prelude Control.Applicative> show <$> show <$> Just 3
Just "\"3\""
Prelude Control.Applicative> pure show <*> pure show <*> Just 3

<interactive>:12:6:
    Couldn't match type `[Char]' with `a0 -> b0'
    Expected type: (a1 -> String) -> a0 -> b0
      Actual type: (a1 -> String) …
Run Code Online (Sandbox Code Playgroud)

syntax haskell infix-notation infix-operator applicative

12
推荐指数
1
解决办法
317
查看次数

哈斯克尔.为什么:info(:)返回两次定义?

我是哈斯凯尔的新手.

如果我输入GHCi(7.10.3):

:info (:)
Run Code Online (Sandbox Code Playgroud)

我得到结果:

*** Parser:
data [] a = ... | a : [a]   -- Defined in ‘GHC.Types’
infixr 5 :

data [] a = ... | a : [a]   -- Defined in ‘GHC.Types’
infixr 5 :
Run Code Online (Sandbox Code Playgroud)

这是否意味着运算符被定义了两次?我在源中找不到任何可疑的东西= /

haskell types infix-notation

12
推荐指数
1
解决办法
155
查看次数

如果方法是中缀和右关联,为什么Scala会为name-by-name参数计算参数?

由于我理解call-by-name了方法的参数,因此在将参数表达式传递给方法时,不会计算相应的参数表达式,但只有在方法体中使用参数的值时(以及如果).

但是,在下面的例子中,这只在前两个方法调用中才有效,但在第三个方法调用中不是这样,尽管它应该只是第二种情况的语法变体!

为什么在第三个方法调用中计算参数表达式?

(我使用Scala 2.11.7测试了此代码)

class Node(x: => Int)

class Foo {
  def :: (x: =>Int) = new Node(x)  // a right-associative method
  def !! (x: =>Int) = new Node(x)  // a left-associative method
}

// Infix method call will not evaluate a call-by-name parameter:
val node = (new Foo) !! {println(1); 1}
println("Nothing evaluated up to here")

// Right-associative method call will not evaluate a call-by-name parameter:
val node1 = (new Foo).::({println(1); 1})
println("Nothing evaluated up to …
Run Code Online (Sandbox Code Playgroud)

scala infix-notation lazy-evaluation callbyname

11
推荐指数
2
解决办法
565
查看次数

如何给lambda中的运算符添加空间?

例如,这不是类型检查

\cons nil -> 5 `cons` 3 `cons` nil
Run Code Online (Sandbox Code Playgroud)

这也不是

\(#) -> 5 # 3 # nil
Run Code Online (Sandbox Code Playgroud)

虽然这两者都有

\cons nil -> 5 `cons` nil
\(#) nil -> 5 # nil
Run Code Online (Sandbox Code Playgroud)

有没有办法在lambdas中为运算符分配infixites.我试过了

infixr 5 #
foo = \(#) nil -> 5 # 3 # nil
Run Code Online (Sandbox Code Playgroud)

这给出了没有定义#和的错误

foo = \(infixr 5 #) nil -> 5 # 3 # nil
Run Code Online (Sandbox Code Playgroud)

这只是一个语法错误.

我能做什么?

syntax lambda haskell infix-notation infix-operator

11
推荐指数
1
解决办法
250
查看次数

C++中缀为逻辑条件的前缀转换

我想用C++评估一个表达式.为了评估它,我希望表达式转换为前缀格式.

这是一个例子

 wstring expression = "Feature1 And Feature2";
Run Code Online (Sandbox Code Playgroud)

这是可能的方法.

 expression = "Feature1 And (Feature2 Or Feature3)";

 expression = "Not Feature1 Or Feature3";
Run Code Online (Sandbox Code Playgroud)

这里,或者,不是保留字和括号(" ( ",))用于范围

没有更高的优先权

设置为Not的优先级

或者设置为And的下一个优先级

WHITE SPACE用于分隔符.Expression 没有其他元素,如TAB,NEWLINE

并不需要算术表达式.我可以做评估,但有人可以帮助我将字符串转换为前缀表示法吗?

c++ expression infix-notation prefix

10
推荐指数
1
解决办法
2872
查看次数

作用域如何影响 infix:&lt;&gt; sub 的 EVAL?

此代码按预期工作:

\n
sub infix:<mean>(*@a) {\n    @a.sum / @a.elems\n}\nsub Mean (*@a) {\n    @a.sum  / @a.elems\n}\n\nsay EVAL 'Mean 2, 6, 4';     # Output: 4\nsay EVAL '2 mean 6 mean 4';  # Output: 4\n
Run Code Online (Sandbox Code Playgroud)\n

当第 7 行在其自己的范围内时,它会按预期工作:

\n
{say EVAL 'Mean 2, 6, 4';}   # Output: 4\n
Run Code Online (Sandbox Code Playgroud)\n

但是当第 8 行在它自己的作用域内时:

\n
{say EVAL '2 mean 6 mean 4';} \n\n===SORRY!=== Error while compiling .../EVAL_1\nTwo terms in a row\nat .../EVAL_1:1\n------> 2\xe2\x8f\x8f mean 6 mean 4\n    expecting any of:\n        infix\n        infix stopper\n        statement end\n …
Run Code Online (Sandbox Code Playgroud)

scope infix-notation subroutine raku

9
推荐指数
1
解决办法
98
查看次数