我已经明白前缀和中缀是等价的.那为什么以下提供不同类型的答案?
3.*(5) //> res50: Double = 15.0
3*5 //> res51: Int(15) = 15
Run Code Online (Sandbox Code Playgroud) 我在包中找到了一堆运算符$||
parallel
:
-- Strategic function application
{-
These are very handy when writing pipeline parallelism asa sequence of
@$@, @$|@ and @$||@'s. There is no need of naming intermediate values
in this case. The separation of algorithm from strategy is achieved by
allowing strategies only as second arguments to @$|@ and @$||@.
-}
-- | Sequential function application. The argument is evaluated using
-- the given strategy before it is given to the function.
($|) :: (a …
Run Code Online (Sandbox Code Playgroud) 如何将负数从中缀转换为后缀?
假设我有一个表达式
a =-b-(-cd)
在某些地方,我读到您可以将诸如
a =(-b)-(-cd)
但是在这里,如果我这样做的话,我将在后缀表达式的开头得到类似“ ab-”的术语,表示ab且不正确。
我该如何转换呢?
我给的函数foldl
是以中缀方式应用的吗?
例
foldl (-) 0 [1,2,3]
= 0-1-2-3
= -6
Run Code Online (Sandbox Code Playgroud)
更一般地说:
foldl f x [a,b,c]
Run Code Online (Sandbox Code Playgroud)
适用于:
(((x `f` a) `f` b) `f` c)
Run Code Online (Sandbox Code Playgroud)
我知道它是递归的,但我可以这样思考吗?
我想要得到的总和seq
的Integer
S IN斯卡拉.
在我看来,我想在这样的整数上加一个加号:
val seqOfIntegers:Seq[Int] = Seq(1, 2, 3, 4, 5)
val sumOfIntegers = seqOfIntegers.reduce(+)
Run Code Online (Sandbox Code Playgroud)
这是无效的.
相反,我必须这样做:
val sumOfIntegers = seqOfIntegers.reduce(plus)
...
def plus(a:Integer, b:Integer): Integer = { a + b}
Run Code Online (Sandbox Code Playgroud)
(我相信你可以加油 - 但我的观点是原始的加号不起作用,错误信息也不清楚原因.)
我的问题是:为什么我不能在Scala中减少(+)seq的整数?
这是我的函数,它应该为*和+操作实现中缀评估.
(defun calculate(l)
(cond
((eql (cadr l) '+) (+ (car l) (cddr l)))
((eql (cadr l) '*) (- (car l) (cddr l)))
)
)
Run Code Online (Sandbox Code Playgroud)
当我用列表'(3 + 4)运行它时,它给出了一个错误,说"(4)不是数字".任何想法可能是什么问题?
我刚刚对值进行了一些验证,以查看它是三的乘积。大量使用模函数。我想管它。很好地使用部分应用程序。但显然不是。
这是我的fsi在vs代码中的一个示例。
> 27 % 3
-
- ;;
val it : int = 0
> (%) 3 27
- ;;
val it : int = 3
Run Code Online (Sandbox Code Playgroud)
我真的没想到从中缀和部分中得到不同的结果。
这是上下文管道中的操作:
...
|> Seq.length // 27
|> (%) 3 // 3
Run Code Online (Sandbox Code Playgroud) 我是 Haskell 的新手,不知道哪个是惯用的 - 使用前缀形式或中缀形式的运算符。从我目前为止发现,有特殊字符功能名称,如<$>
,<*>
,>>=
等在中缀形式,但功能,其名称由字母最多的前缀形式使用使用。我的猜测是这是从数学中获得的灵感,尽管我不确定。
我查过Blow your mind , Category:Idioms并搜索了 stackoverflow ,但无济于事。
澄清一下,什么时候中缀函数和前缀运算符是惯用的?
实际上,我有几个关于此代码片段的问题:
object InfixTypesHard2 extends App {
val x0: Int --- String --- Boolean = ???
}
class ---[T, V](t: T, v: V)
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
Exception in thread "main" scala.NotImplementedError: an implementation is missing
at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
at section3_OOP_operatorOverloading.InfixTypesHard2$.delayedEndpoint$section3_OOP_operatorOverloading$InfixTypesHard2$1(InfixTypesHard2.scala:5)
at section3_OOP_operatorOverloading.InfixTypesHard2$delayedInit$body.apply(InfixTypesHard2.scala:3)
at scala.Function0.apply$mcV$sp(Function0.scala:39)
at scala.Function0.apply$mcV$sp$(Function0.scala:39)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
at scala.App.$anonfun$main$1(App.scala:76)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at section3_OOP_operatorOverloading.InfixTypesHard2$.main(InfixTypesHard2.scala:3)
at section3_OOP_operatorOverloading.InfixTypesHard2.main(InfixTypesHard2.scala)
Run Code Online (Sandbox Code Playgroud)