我正在阅读以下数据类型:
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)
什么是:..在第二个成员声明中?
假设用户输入中缀表达式作为字符串?使用C语言评估表达式结果的最简单方法(最简单的意思是短语 t)是什么?
可能的方法是将其转换为后缀然后使用stacks.But它相当长的过程.有没有办法使用像atoi()或eval()这样的函数来简化工作?
在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
我在玩同infixr,infixl和infix声明.我知道如何infixr和infixl工作原理:
-- 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) 为什么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) 我是哈斯凯尔的新手.
如果我输入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)
这是否意味着运算符被定义了两次?我在源中找不到任何可疑的东西= /
由于我理解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) 例如,这不是类型检查
\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)
这只是一个语法错误.
我能做什么?
我想用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
我并不需要算术表达式.我可以做评估,但有人可以帮助我将字符串转换为前缀表示法吗?
此代码按预期工作:
\nsub 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\nRun Code Online (Sandbox Code Playgroud)\n当第 7 行在其自己的范围内时,它会按预期工作:
\n{say EVAL 'Mean 2, 6, 4';} # Output: 4\nRun 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) infix-notation ×10
haskell ×5
expression ×2
scala ×2
syntax ×2
applicative ×1
c ×1
c++ ×1
callbyname ×1
constructor ×1
dictionary ×1
evaluate ×1
lambda ×1
math ×1
parentheses ×1
prefix ×1
raku ×1
scope ×1
subroutine ×1
types ×1