我对 Haskell 很陌生。我已经挣扎了很长时间,并尝试了我能想到的一切。我希望该函数执行的是检查第一个非零元素,将其减 1,然后根据它在列表中的位置(最后一个元素从位置 1 开始)增加下一个元素。例如:
示例 1:
[9,0,0,0,0,0,0,0,0] -> [8,8,0,0,0,0,0,0,0] increments the next element by 8 since it's at position 8
Run Code Online (Sandbox Code Playgroud)
示例 2:
[0,0,0,0,0,0,0,3,1] -> [0,0,0,0,0,0,0,2,2] increments the next element by 1 since it's at position 1
Run Code Online (Sandbox Code Playgroud)
示例 3:
[0,0,3,2,0,0,0,0,0] -> [0,0,2,8,0,0,0,0,0] increments the next element by 6 since it's at position 6
Run Code Online (Sandbox Code Playgroud)
我的代码适用于上述所有情况,但最后一个元素不为零的情况除外。例如:
[0,0,0,0,0,0,0,0,5] should return [0,0,0,0,0,0,0,0,4] but it gives me the error 'empty list.'
Run Code Online (Sandbox Code Playgroud)
我知道我需要有一个条件来检查我的列表的长度是否为 1,如果是,它应该只将当前元素减一(并且不增加下一个元素,因为没有一个)。我只是不知道该怎么做。到目前为止,这是我的代码:
chop :: [Int] -> [Int]
chop [] = []
chop (x:xs) …Run Code Online (Sandbox Code Playgroud) 我怎样才能做到以下几点。例如,如果我想减去Just 8-Just 5得到Just 3,我该怎么做?
Just 8 - Just 5 = Just 3
Just 15 - Just 9 = Just 6
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 docker 容器运行一个简单的 hello world 程序。我想要做的是显示我传递给环境变量的自定义消息。如果没有传递给环境变量,那么程序应该只显示“Hello World!”的默认消息。我已经在我的 dockerfile 中创建了一个环境变量,我正在尝试使用 --env-file 标志在我的环境变量文件中覆盖该变量。但是,我不确定设置环境变量的正确语法是什么,因为我得到了“无效语法”。
以下是我的文件:
文件
FROM python:3
ADD main.py /
ENV MESSAGE "Hello World!"
CMD ["python3", "./main.py"]
Run Code Online (Sandbox Code Playgroud)
环境文件
MESSAGE="Goodbye World!"
Run Code Online (Sandbox Code Playgroud)
主文件
# simple hello world program
print($MESSAGE)
Run Code Online (Sandbox Code Playgroud)
这就是我构建和运行容器的方式
docker build -t example -f Dockerfile .
docker run --env-file=env_file --rm --name example example
Run Code Online (Sandbox Code Playgroud)