我在尝试跟踪函数中的计数器变量时遇到了一些困难.
我创建了一个函数,它将一个数字作为单个参数,并递归地将该数字乘以2,将所有数字相乘的两个相加,下面的代码使我更清楚我打算做什么:
sum :: Float -> Float
sum x = x + sum (2*x)
Run Code Online (Sandbox Code Playgroud)
但是,我面临的困难是,我希望这个函数只递归十次.所以我希望一旦添加十个数字就停止它.我尝试使用计数器来跟踪函数递归的次数,但无济于事.
这是我试过的:
sumTen :: Float -> Float
sumTen x n
where
n=10
|n == 0 = x
|otherwise = x + sumTen (2*x) (n-1)
Run Code Online (Sandbox Code Playgroud)
我意识到上面的代码不起作用,计数器n将通过每次递归调用给出值十,这意味着它永远不会达到基本情况n == 0.
使这如此困难的原因是sumTen必须使用一个参数调用.在上面的代码中,我试图给函数一个n带有预定值的默认参数,但它显然不起作用.
有人可以帮助我在'n'递归调用后使递归函数停止吗?
我需要编写一个函数来接受函数列表和一个值作为参数.必须依次对值中的每个函数应用.
例如,如果我的函数被调用compFuncs...
compFuncs [f,g,h] val 相当于 f(g(h val))
我已经可以告诉我使用a foldr在这里很有用,我可以将.运算符放在函数列表中的每个函数之间,然后将其应用到val.但是,我无法完成它,这是我的尝试......
compFuncs :: [(a->a->a)] -> a -> a
compFuncs [] val = val
compFuncs (x:xs) val = foldr //Im lost here
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
我用来executeScript在当前活动选项卡中运行。但在其回调函数内,我想向正在执行的脚本发送一条消息......
chrome.tabs.executeScript(null, {
file: 'src/js/scripts/extractCSS.js'
}, function() {
chrome.tabs.sendMessage(this.props.source);
Run Code Online (Sandbox Code Playgroud)
this.props.source是我试图传递的一个对象。我在内心src/js/scripts/extractCSS.js试图捕捉到这个信息......
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
});
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误...
Error in response to tabs.executeScript: Error: Invocation
of form tabs.sendMessage(object) doesn't match definition
tabs.sendMessage(integer tabId, any message, optional object
options, optional function responseCallback)
Run Code Online (Sandbox Code Playgroud)
根据我收集的信息,我需要定义tabId,但我只需要将消息发送到活动选项卡。我尝试添加nullfortabId但它仍然给我一个错误。
我怎样才能解决这个问题?
如何使用带有grep的正则表达式打印所有不包含模式的行.我无法使用,-v因为我想在更复杂的一个中实现正则表达式,因为这-v是不切实际的.
但每当我尝试打印不包含模式的行时,我都无法获得预期的输出.
例如,如果我有这个文件:
blah blah blah
hello world
hello people
something
Run Code Online (Sandbox Code Playgroud)
我想要打印所有不包含的行hello,输出应该是:
blah blah blah
something
Run Code Online (Sandbox Code Playgroud)
我试过这样的东西,但它不起作用:
egrep '[^hello]' file
Run Code Online (Sandbox Code Playgroud)
关于SO使用的所有答案-v,我找不到使用正则表达式的答案
我正在尝试按照以下说明将haskell-mode添加到emacs:
http://doc.gnu-darwin.org/haskell-mode/installation-guide.html
这涉及到我在~/.emacsinit文件中添加了一些代码.但是,我的问题是我无法找到我的emacs init文件.我尝试使用find命令来定位它,如下所示:
find . -name "*emacs*"
find . -name "~/.emacs"
Run Code Online (Sandbox Code Playgroud)
然而,这些似乎都不是很成功,因为我得到的结果太多,或者没有结果.
所以,鉴于我的情况,因为我无法找到我的~/.emacsinit文件,这是否意味着它不存在?在这种情况下,使用emacs编辑器自己创建一个是否聪明?如果是这样,在尝试创建之前我是否应该知道任何未完成的事情?
我有以下功能:
tempFunc :: Int-> Int-> Int
tempFunc x y
| y == 0 = 0
| x `mod` y == 0 = y + tempFunc x (y-1)
| otherwise = y-1
Run Code Online (Sandbox Code Playgroud)
其目的是递归地将所有数字因子相加.我想消除对第二个参数的需要y(因为y它等于x),所以我以下面的方式实现了这个功能
tempFunc :: Int-> Int-> Int
sumFactor num = tempFunc num num
where
tempFunc x y
...
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
The type signature for ‘tempFunc’ lacks an accompanying binding
Run Code Online (Sandbox Code Playgroud)
我注意到,当类型定义不正确时会出现这种类型的错误.但我无法弄清楚我的类型定义有什么问题,因为第一个摘录有效.
如果所有项都大于1,我需要返回布尔值True,否则返回False.这是我的尝试......
greaterOne :: Ord a=>a->Bool
greaterOne x = x > 1
checkLis :: Ord a=>[a]->Bool
checkLis [] = True
checkLis (x:xs)
| greaterOne x = checkLis xs
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误,我一直试图解决它,但无济于事......
Could not deduce (Num a) arising from the literal ‘1’
from the context (Ord a)
bound by the type signature for greaterOne :: Ord a => a -> Bool
at pract.hs:23:15-28
Possible fix:
add (Num a) to the context of
the type signature for greaterOne :: Ord a …Run Code Online (Sandbox Code Playgroud) 我想在字符串中获取一组匹配项。目前,我的字符串是媒体查询。我删除了媒体查询中的所有新行,因此一个媒体查询可能如下所示:
@media (max-width: 1200px) { .container { padding-left: 0px; } .bundles {padding-right:10px} }
Run Code Online (Sandbox Code Playgroud)
我想要做的是从媒体查询中获取所有类,包括它们的样式属性。所以我想要一个如下所示的数组:
[".container { padding-left: 0px; }", ".bundles {padding-right:10px}"]
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
var identifiers = mediaQuery.match(/\.(.*)}/);
Run Code Online (Sandbox Code Playgroud)
我当时假设match会给我所有比赛。但是我只得到了一场比赛,所以我一定是做错了什么。
我正在使用 提供的 DNNtflearn从一些数据中学习。我的data变量的形状为(6605, 32),我的labels数据的形状为(6605,),我在下面的代码中将其重塑为(6605, 1)......
# Target label used for training
labels = np.array(data[label], dtype=np.float32)
# Reshape target label from (6605,) to (6605, 1)
labels = tf.reshape(labels, shape=[-1, 1])
# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)
# DNN
net = tflearn.input_data(shape=[None, 32])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 1, activation='softmax')
net = tflearn.regression(net)
# Define model.
model …Run Code Online (Sandbox Code Playgroud) machine-learning neural-network python-3.x tensorflow tflearn
我曾经VictoryChart实现过一个饼图,但效果很好...
render() {
const data_distribution = this.state.pie_keys.map((d) => ({x:d, y:this.state.pie_data[d], label:d }));
return (
<div className="App">
<div className="pie_wrapper">
<VictoryPie
labelComponent={<VictoryTooltip cornerRadius={0} />}
padAngle={0.5}
innerRadius={100}
width={400} height={400}
style={{
labels: { fontSize: 15, fill: "black"},
data: {
fillOpacity: 0.9, stroke: "white", strokeWidth: 3
}
}}
labelRadius={90}
data = {data_distribution}
/>
</div>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
重要的是要注意,其data_distribution外观如下所示...
data={[
{ x: "Fac of Engineering & Appl Sci", y: 8.557902403495994 },
{ x: "Faculty of Arts and Science", y: 53.775188152464196 },
{ …Run Code Online (Sandbox Code Playgroud) haskell ×4
javascript ×2
regex ×2
emacs ×1
function ×1
grep ×1
haskell-mode ×1
init ×1
list ×1
pie-chart ×1
python-3.x ×1
reactjs ×1
recursion ×1
tensorflow ×1
tflearn ×1