有些编译器支持pure和const,但是有什么要求检查这些断言是否成立?例如:
int global_value = 42;
const int global_value_const = 42;
int MyPureFunction __attribute__ (( pure_check? )) (
int input,
int* input_ptr,
const int* input_const_ptr,
int foo& input_ref,
const int& input_const_ref)
{
int temporary = input; // Valid, can read local but mutable state.
global_value += temporary; // Invalid, cannot mutate external state
temporary += global_value; // Invalid, cannot read non-const global data.
temporary += global_value_const; // Valid, can read const global data.
temporary += *input_ptr; // Invalid, cannot …Run Code Online (Sandbox Code Playgroud) 在Haskell中,如何将x个数字列表更改为n个n个数字列表?
第一个子列表的编号从第一到第十,第二个列表从第11到第20 ......
myFunction :: [Int] - > [[Int]]
haskell functional-programming function list purely-functional
我正在解决一个类似这样的编程练习:
文件包含正整数和之间的等式列表,每行一个,每个以分号结尾,没有空格.这些平等可能是对的,也可能是错的.例如,请考虑以下文件:
Run Code Online (Sandbox Code Playgroud)2+3+12=9+8; 2+3+4=9; 22=3+4+5+10; 3+5+1=4+44;编写一个程序,将文件名作为其第一个参数,并输出正确行的比例.例如,给定上面的文件,程序应输出0.75.
我在Scala中的解决方案如下,它可以工作,但我正在寻找一种在没有var的情况下重写它的方法.
import scala.io.Source
object Hello {
def main(args: Array[String]): Unit = {
var count: Int = 0
var correct: Int = 0
Source.fromFile(args(0)).getLines().zipWithIndex.foreach { case (line, i) =>
val regex = """^[1-9][0-9]*(\+[1-9][0-9]*)*=[1-9][0-9]*(\+[1-9][0-9]*)*;$""".r
regex findFirstIn line match {
case Some(_) =>
case None => throw new Exception("Error reading file, line " + i)
}
val sums = line.substring(0, line.length - 1).split('=').map {
_.split('+').map(Integer.parseInt).sum
}
count += 1
correct += (if (sums(0) == sums(1)) 1 …Run Code Online (Sandbox Code Playgroud) 我花了很长时间没有编程Haskell,并决定通过采用一个相对先进的项目回到它.我正在尝试按照本指南从头开始编程神经网络.我已经摸索了一些他最神秘的方法来解决诸如创建权重和偏见网络之类的简单问题,但是当涉及到这个时:
feed :: [Float] -> [([Float], [[Float]])] -> [Float]
feed input brain = foldl' (((relu <$>) . ) . zLayer) input brain
Run Code Online (Sandbox Code Playgroud)
我不明白他做了什么.更具体地说,我不明白为什么.在这里使用函数组合中的两个.他使用(relu <$>) . ).这.后面的括号对我来说没有意义.我理解它代表函数组合,在这种情况下,函数zLayer接受一层神经元,它是类型([Float], [[Float]])和前一层的输出,它是类型[Float],并产生一个新的输出,也是类型[Float].我理解他正在将relu <$>函数应用于结果zLayer,这是有道理的.也就是说,你想要通过在大脑层上应用来折叠大脑(这只是一个层列表)zLayer,然后应用relu <$>其结果,最后将其作为input下一层传递.
看似空洞的构图是我的错.对我来说,上面描述的内容应该像这样实现:
feed :: [Float] -> [([Float], [[Float]])] -> [Float]
feed inp brain = foldl' (((sigmoid <$>) . computeLayer) inp brain
Run Code Online (Sandbox Code Playgroud)
(我使用的是sigmoid函数而不是整流器(ReLU),而computeLayer只是我对zLayer的实现.)对吗?我在那里做的是(据说)提供,作为一个功能foldl',这:
(sigmoid <$> (computeLayer))
Run Code Online (Sandbox Code Playgroud)
当我 …
haskell functional-programming purely-functional neural-network function-composition
我在 Scala 中编写了几行代码,但不知道如何使用不可变变量 (val) 使同样的事情工作。任何帮助都感激不尽。
class Test {
def process(input: Iterable[(Double, Int)]): (Double, Int) = {
var maxPrice: Double = 0.0
var maxVolume: Int = 0
for ((price, volume) <- input) {
if (price > maxPrice) {
maxPrice = price
}
if (volume > maxVolume) {
maxVolume = volume
}
}
(maxPrice, maxVolume)
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我将所有 var 转换为 val 并使其更具功能性吗?提前致谢!:)
functional-programming scala immutability purely-functional scala-collections
如何在没有任何变量的情况下从地图上更改以下键全功能
HashMap(false -> List(20, 15, 20, 17), true -> List(50, 25, 45, 21, 100, 2000, 2100))
Run Code Online (Sandbox Code Playgroud)
到
HashMap("String1" -> List(20, 15, 20, 17), "String2" -> List(50, 25, 45, 21, 100, 2000, 2100))
Run Code Online (Sandbox Code Playgroud)
我尝试使用地图,并且能够将键更改为相同的字符串,但不能更改为不同的字符串。
当我阅读SDLhaskell 的文档时,我发现一些函数不可避免地修改了它的输入.例如,blitSurface将目标曲面作为输入,但在函数内更新.现在,概括问题,如果我有一个函数f :: a -> IO a,如果我a在函数内修改它会破坏组合吗?怎么样f :: IO a -> IO a?怎么样a -> IO ()?那怎么样IO a -> IO ()?
考虑到blitSurface实际上是外来函数的情况,并且每帧的新表面听起来效率不高,这些函数很难避免.这些功能会导致更大规模的问题吗?例如,使用fModifySurface :: Surface -> IO ()破坏性更新作为示例:
main = do
w <- ... -- The window surface
-- Do something here
s <- someFuncGetSurface -- We get a surface here
fModifySurface s -- Destructively update s
blitSurface ...... -- Ignore the actual API, …Run Code Online (Sandbox Code Playgroud) 我有Merge Sort的这种实现:
import scala.annotation.tailrec
object MergeSort {
def sortBy[T]: ((T, T) => Int) => Seq[T] => Seq[T] = comparator => seqToSort => {
@tailrec
def merge(xs : Seq[T], ys : Seq[T], accum : Seq[T] = Seq()) : Seq[T] = (xs, ys) match {
case (Seq(), _) => ys ++ accum
case (_, Seq()) => xs ++ accum
case (x::rx, y::ry) =>
if(comparator(x, y) < 0)
merge(xs, ry, y +: accum)
else
merge(rx, ys, x +: accum)
}
@tailrec
// Problem …Run Code Online (Sandbox Code Playgroud) recursion functional-programming scala tail-recursion purely-functional