我在haskell中编写一个函数来检查某些东西是否是列表的元素.
我有以下代码:
elementOf :: Eq a => a ->[a] -> Bool
elementOf _ [] = False
elememtOf x (y:ys) = (x==y) || elementOf x ys
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Pattern match(es) are non-exhaustive
In an equation for ‘elememtOf’: Patterns not matched: _ []
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到这样的错误,因为我有_ []的情况.
谁能解释一下我哪里出错了?
谢谢!
haskell functional-programming compiler-errors pattern-matching
我有一个获取列表并必须返回其最小元素的函数。
不幸的是,我一直遇到这个问题:
解析模式错误:最小
我做错了什么?
minim :: [Int] -> Int
minim [] = 0
minim [x] = x
minim x:xs = min x (minim xs)
min :: Int -> Int -> Int
min a b
| a > b = b
| a < b = a
Run Code Online (Sandbox Code Playgroud) recursion haskell functional-programming pattern-matching parse-error
这是我的项目:
project
|--- main.cpp
|--- makefile
|--- test
|--- Test.cpp
|--- Test.h
Run Code Online (Sandbox Code Playgroud)
这是makefile:
g++1x:=g++ -std=c++14 -stdlib=libc++ -MMD -MP
cflags:= -Wall -lncurses
PATHS:=./ ./test/
TARGET:=matrix.out
SRC:=$(foreach PATH,$(PATHS),$(wildcard $(PATH)/*.cpp))
OBJDIR:=.obj
OBJ:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.o)))
.PHONY: install
install: $(OBJDIR) $(TARGET)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(TARGET): $(OBJ)
$(g++1x) $(cflags) -o $@ $^ -g
$(OBJDIR)/%.o: %.cpp
$(g++1x) -c -o $@ $< -g
$(OBJDIR)/%.o: ./test/%.cpp
$(g++1x) -c -o $@ $< -g
-include $(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.d)))
.PHONY: clean
clean:
rm -f $(TARGET)
rm -rf $(OBJDIR)
Run Code Online (Sandbox Code Playgroud)
它运作良好,但我有两个问题: …
我怎样才能确定模式
func (2:xs) = expression
Run Code Online (Sandbox Code Playgroud)
其中2:xs是长度为2的列表与模式不匹配
func (2:x:xs) = expression2
Run Code Online (Sandbox Code Playgroud)
其中2:x:xs是长度为3的列表?
我试图在haskell中创建一个sum函数.我这样做是为了更熟悉这门语言.我把它定义为:
mysum :: [Integer] -> Integer
mysum lst = sm lst
where
sm :: [Integer] -> Integer
sm lst [] = 0
sm lst [x:xs]=
x + sm xs
Run Code Online (Sandbox Code Playgroud)
我的想法是返回列表头部的值,+尾部反馈到函数中.我记得在F#中做了类似的事情,但我简直无法让它在haskell中工作.
The error im getting is:
sum.hs:5:5: error:
• Couldn't match expected type ‘Integer’
with actual type ‘[[Integer]] -> Integer’
• The equation(s) for ‘sm’ have two arguments,
but its type ‘[Integer] -> Integer’ has only one
In an equation for ‘mysum’:
mysum lst
= sm lst
where
sm :: [Integer] …Run Code Online (Sandbox Code Playgroud) 我知道(:%s/apple/orange/g)在vim中的简单搜索和替换命令,我们找到所有'苹果'并用'orange'替换它们.
但是有可能在vim中做这样的事情吗?在文件中找到所有'Wheat'并在跳过下一个单词后添加"store"(如果有的话)?
示例:原始文件内容:
Wheat flour
Wheat bread
Rice flour
Wheat
Run Code Online (Sandbox Code Playgroud)
搜索和替换后:
Wheat flour store
Wheat bread store
Rice flour
Wheat store
Run Code Online (Sandbox Code Playgroud) Hy家伙,我有一个以"s"开头的文件列表,其中一些以"m"结尾,而另一些以"i"结尾.我想用"m"来完成那些但是这个命令不起作用.
list=list.files(path="~/Deduplicated_input",pattern="^sorted_deduplicated_sorted*m$", full.names=TRUE)
Run Code Online (Sandbox Code Playgroud)
你怎么看?
我有一个OCaml函数,它返回表中指定单元格的值.该功能正常,但我仍然收到警告:
警告8:此模式匹配并非详尽无遗.以下是不匹配的值的示例:([],_)
即使我实际上在我的实现中占了这个值:
let cell_value([i;j],table) = match ([i;j],table) with
([],_) -> []
| (_,[]) -> []
| (_::_,_::_) -> List.nth (List.nth table (j-1)) (i-1);;
Run Code Online (Sandbox Code Playgroud)
就像我说的,函数返回正确的值,我只是想摆脱错误.我仍然是OCaml的新手,所以任何帮助将不胜感激!
Just在分解模式时我应该如何使用构造函数?
EG:
如果我的模式是: (x1,x2,x3,....xn) 我将必须用它包围模式的每个元素Just?
我的问题:我正在尝试"安全地"实现Init功能.
我是否必须Just在第二行使用尾部和头部?
safeInit::[a]->Maybe [a]
safeInit (x:xs)=x: safeInit (Just xs) #Just x : safeInit Just xs ?
safeInit [x,y]=Just [x]
safeInit _ =Nothing
Run Code Online (Sandbox Code Playgroud) 我编写了以下基于模式匹配的函数:
def replacingElem(lines: List[String], last10: String): List[String] =
{
if (lines.isEmpty) Nil else {
val row = lines.head.split("[ \t]+")
row(0).toInt match {
case 10 => lines.head :: replacingElem(lines.tail, row(1))
case 15 => (row(0) + " " + last10 + " " + row(2) + " " + row(3)) :: replacingElem(lines.tail, last10)
case _ => lines.head :: replacingElem(lines.tail, last10)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是使整数10和15可调,并使它们成为函数的参数.我做了以下修改:
def replaceIndex(lines: List[String], last10: String,a:Int,b:Int): List[String] = {
if (lines.isEmpty) Nil else {
val row = lines.head.split("[ \t]+") …Run Code Online (Sandbox Code Playgroud)