Haskell where语句返回输入的解析错误‘=’;

Haskell where语句返回输入的解析错误‘=’;,haskell,Haskell,我创建的这个函数在输入“=”时返回一个解析错误 checkappend :: Maybe [a] -> Maybe [a] -> (a -> Bool) -> Maybe [a] checkappend ml1 ml2 test = if all test l1 then (Just (l1 ++ l2)) else Nothing where l1 = fromJust(ml1) l2 = fromJust(ml2) ,

我创建的这个函数在输入“=”时返回一个
解析错误

checkappend :: Maybe [a] -> Maybe [a] -> (a -> Bool) -> Maybe [a]
checkappend ml1 ml2 test =
    if all test l1
    then (Just (l1 ++ l2))
    else Nothing
    where l1 = fromJust(ml1)
        l2 = fromJust(ml2)

,其中
需要缩进小于函数体,任何:

checkappend ml1 ml2 test =
    if all test l1
     then Just (l1 ++ l2)
     else Nothing
  where l1 = fromJust ml1
        l2 = fromJust ml2
顺便说一句,使用像
head
fromJust
这样的部分函数是不好的风格。最好在
参数上使用模式匹配!另外,
如果
那么
否则
在Haskell有些不受欢迎–警卫看起来更漂亮:

checkappend (Just l1) (Just l2) test
  | all test l1  = Just $ l1 ++ l2
  | otherwise    = Nothing
当然,模式匹配仍然不完整。。。如果其中一个参数是
Nothing
,该怎么办?我想你也希望结果是什么都不是;这是可能的,有一个全面的模式:

checkappend (Just l1) (Just l2) test
  | all test l1   = Just $ l1 ++ l2
checkappend _ _ _ = Nothing

请注意,这已经包括
|否则
保护:如果给定模式子句的保护不匹配,Haskell只会继续下一个子句。

其中的
需要缩进小于函数体,任何:

checkappend ml1 ml2 test =
    if all test l1
     then Just (l1 ++ l2)
     else Nothing
  where l1 = fromJust ml1
        l2 = fromJust ml2
顺便说一句,使用像
head
fromJust
这样的部分函数是不好的风格。最好在
参数上使用模式匹配!另外,
如果
那么
否则
在Haskell有些不受欢迎–警卫看起来更漂亮:

checkappend (Just l1) (Just l2) test
  | all test l1  = Just $ l1 ++ l2
  | otherwise    = Nothing
当然,模式匹配仍然不完整。。。如果其中一个参数是
Nothing
,该怎么办?我想你也希望结果是什么都不是;这是可能的,有一个全面的模式:

checkappend (Just l1) (Just l2) test
  | all test l1   = Just $ l1 ++ l2
checkappend _ _ _ = Nothing

注意,这已经包括了
|否则
保护:如果给定的pattern子句没有匹配的保护,Haskell只会继续下一个子句。

一种非常巧妙的编写方法:

import Control.Monad (guard)

checkAppend m1 m2 test =
  do
    l1 <- m1
    l2 <- m2
    guard $ all test l1
    return $ l1 ++ l2
import Control.Monad(守卫)
检查m1 m2测试=
做

l1一种非常巧妙的写作方式:

import Control.Monad (guard)

checkAppend m1 m2 test =
  do
    l1 <- m1
    l2 <- m2
    guard $ all test l1
    return $ l1 ++ l2
import Control.Monad(守卫)
检查m1 m2测试=
做

l1谢谢,这行代码做了什么呢?
checkappend\uuuuuu=Nothing
,与DanielKobe有什么不同?
otherse=Nothing
otherse=Nothing
不处理一个或两个参数都是
Nothing
的情况
u
是一种通配符模式,它匹配任何内容并忽略它。如果
所有测试l1
保护失败,匹配者将尝试下一行
检查附加
,该行将成功。谢谢,这一行做什么
检查附加
=无
,与
否则=无
有什么不同,
否则=Nothing
不处理一个或两个参数为
Nothing
的情况
u
是一种通配符模式,它匹配任何内容并忽略它。如果
所有测试l1
保护失败,匹配器将尝试下一行
检查附加
,该行将成功。