如何实现 fromJust 并遍历 Haskell 中的字符串列表

How to implement fromJust and iterate over a list of strings in Haskell

所以我得到了这些功能:

intercalate' :: [a] -> [[a]] -> [a]
intercalate' xs xss = concat (intersperse' xs xss)

intersperse'             :: a -> [a] -> [a]
intersperse' _   []      = []
intersperse' sep (x:xs)  = x : prependToAll' sep xs

prependToAll'            :: a -> [a] -> [a]
prependToAll' _   []     = []
prependToAll' sep (x:xs) = sep : x : prependToAll' sep xs


encodeWord :: Table -> String -> Maybe Code
encodeWord table str = intercalate' [Silence, Silence]
    <$> mapM (\x -> lookup x table) str

对于此数据:

module Types where

data Atom = Beep | Silence
  deriving (Eq, Show)

type Code = [Atom]

dit, dah, shortGap, mediumGap :: Code
dit       = [Beep, Silence]
dah       = [Beep, Beep, Beep, Silence]
shortGap  = replicate (3-1) Silence
mediumGap = replicate (7-1) Silence

morseCode :: Char -> Code
morseCode 'A' = dit ++ dah
morseCode 'B' = dah ++ dit ++ dit ++ dit
morseCode 'C' = dah ++ dit ++ dah ++ dit
morseCode 'D' = dah ++ dit ++ dit
morseCode 'E' = dit
morseCode 'F' = dit ++ dit ++ dah ++ dit
morseCode 'G' = dah ++ dah ++ dit
morseCode 'H' = dit ++ dit ++ dit ++ dit
morseCode 'I' = dit ++ dit
morseCode 'J' = dit ++ dah ++ dah ++ dah
morseCode 'K' = dah ++ dit ++ dah
morseCode 'L' = dit ++ dah ++ dit ++ dit
morseCode 'M' = dah ++ dah
morseCode 'N' = dah ++ dit
morseCode 'O' = dah ++ dah ++ dah
morseCode 'P' = dit ++ dah ++ dah ++ dit
morseCode 'Q' = dah ++ dah ++ dit ++ dah
morseCode 'R' = dit ++ dah ++ dit
morseCode 'S' = dit ++ dit ++ dit
morseCode 'T' = dah
morseCode 'U' = dit ++ dit ++ dah
morseCode 'V' = dit ++ dit ++ dit ++ dah
morseCode 'W' = dit ++ dah ++ dah
morseCode 'X' = dah ++ dit ++ dit ++ dah
morseCode 'Y' = dah ++ dit ++ dah ++ dah
morseCode 'Z' = dah ++ dah ++ dit ++ dit
morseCode '1' = dit ++ dah ++ dah ++ dah ++ dah
morseCode '2' = dit ++ dit ++ dah ++ dah ++ dah
morseCode '3' = dit ++ dit ++ dit ++ dah ++ dah
morseCode '4' = dit ++ dit ++ dit ++ dit ++ dah
morseCode '5' = dit ++ dit ++ dit ++ dit ++ dit
morseCode '6' = dah ++ dit ++ dit ++ dit ++ dit
morseCode '7' = dah ++ dah ++ dit ++ dit ++ dit
morseCode '8' = dah ++ dah ++ dah ++ dit ++ dit
morseCode '9' = dah ++ dah ++ dah ++ dah ++ dit
morseCode '0' = dah ++ dah ++ dah ++ dah ++ dah
morseCode  _  = undefined -- Avoid warnings

type Table = [(Char, Code)]

morseTable :: Table
morseTable = [ (c , morseCode c) | c <- ['A'..'Z']++['0'..'9'] ]

encodeWord 函数按预期工作。

样本:输入:"HELLO"输出:[Beep,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Beep,Beep,Silence]

现在,我正在尝试定义一个新函数:encodeWords。

样本:输入:["HI","THERE"]输出:[Beep,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Silence,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence]

到目前为止,我已经做到了。

encodeWords :: Table -> [String] -> Maybe Code
encodeWords table stringList = intercalate' [Silence, Silence, Silence,Silence,Silence,Silence]
    <$> mapM (\x -> encodeWord table x ) stringList

我希望最终函数具有类型签名:

Table -> [String] -> Code

对于encodeWord,我只成功编程了Table -> [String] -> Maybe Code。我试过像这样使用 fromJust

import Data.Maybe    
encodeWord :: Table -> String -> Code
encodeWord table str = fromJust (intercalate' [Silence, Silence]
    <$> mapM (\x -> lookup x table) str)

这有效,但是,我只能将 Prelude 和 Data.Char 用于我正在编写的程序。

当我尝试时:

fromJust          :: HasCallStack => Maybe a -> a
fromJust Nothing  = error "Maybe.fromJust: Nothing" -- yuck
fromJust (Just x) = x

type HasCallStack = (?callStack :: CallStack) 

encodeWord :: Table -> String -> Code
encodeWord table str = fromJust (intercalate' [Silence, Silence]
        <$> mapM (\x -> lookup x table) str)

我刚收到这个错误:

   Operator applied to too few arguments: ?
   |
59 | type HasCallStack = (?callStack :: CallStack)
   |                      ^
Failed, one module loaded.

有什么简单的方法可以让我自己实现 fromJust,只需要使用 Data.Char 和 Prelude 吗?

总而言之,我正在尝试自己实现 fromJust 并使此功能正常工作:

encodeWords :: Table -> [String] -> Maybe Code
encodeWords table stringList = intercalate' [Silence, Silence, Silence,Silence,Silence,Silence]
    <$> mapM (\x -> encodeWord table x ) stringList

我不确定是否应该 MapMapM 或其他东西来将 encodeWord 应用于列表中的每个字符串。之后,encodeWords 应该在输入列表中的每个字符串的输出 Code 之间添加 6 Silences。

你写的 fromJust 的定义,直接来自 Data.Maybe。如果不进行简单的修改,这将无法工作:

fromJust :: Maybe a -> a
fromJust Nothing = error "fromJust: Nothing"
fromJust (Just x) = x

如您所见,fromJust 模式匹配 Maybe 数据类型的构造函数,就像您对 morseCode.

所做的一样

有关模式匹配的更多信息,请参阅:http://learnyouahaskell.com/syntax-in-functions

如果出现问题,return Nothing 是一个非常可行的错误管理策略,您可以级联它。

最后,你总可以把结果通过fromJust,如果有非法输入,后果自负。正如 HaskellFreak 所提到的,您的 fromJust 版本看起来不错。

此代码似乎有效:

myFromJust :: Maybe a -> a
myFromJust  Nothing   =  error "Maybe.fromJust: Nothing" -- yuck
myFromJust  (Just x)  =  x
    
encodeWord :: Table -> String -> Maybe Code
encodeWord table str = (intercalate' [Silence, Silence]
                          <$> mapM (\x -> lookup x table) str)

ghci下测试:

 λ> 
 λ> encodeWord morseTable "ABC"
Just [Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Silence]
 λ> 
 λ> 
 λ> encodeWord morseTable "ABC+"
 Nothing
 λ>

现在正在尝试编写更高级别的函数 encodeWords:

如果我们从一个简单的 map 结构开始:

 λ> 
 λ> stringList = ["ATTACK","AT","DAWN"]
 λ> 
 λ> :type (map (encodeWord morseTable) stringList)
 (map (encodeWord morseTable) stringList) :: [Maybe Code]
 λ>

所以我们有一个 [Maybe Code] 对象。鉴于 intercalate' 的类型签名,我们更喜欢 Maybe [Code] 一个。

 λ> 
 λ> :type mapM
mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
 λ> 

所以如果类型am b相同,最左边的函数参数取id,我们看到mapM可以交换列表用 Maybe 构造。比如:(m b -> m b) -> t (m b) -> m (t b).

 λ> 
 λ> :type (mapM id)
(mapM id) :: (Traversable t, Monad m) => t (m b) -> m (t b)
 λ> 
 λ> :type (mapM id $ map (encodeWord morseTable) stringList)
 (mapM id $ map (encodeWord morseTable) stringList) :: Maybe [Code]
 λ> 
 λ> 
 λ> sil6 = replicate 6 Silence
 λ> 
 λ> :type  ((intercalate' sil6) <$> mapM id (map (encodeWord morseTable) stringList))
  ((intercalate' sil6) <$> mapM id (map (encodeWord morseTable) stringList))
  :: Maybe [Atom]
 λ> 

所以这可能是 encodeWords 的有效代码:

encodeWords :: Table -> [String] -> Maybe Code
encodeWords table stringList =
    let  sil6 = replicate 6 Silence
    in
         intercalate' sil6
             <$>  (mapM id  $  map (encodeWord table) stringList)

ghci下测试:

 λ> 
 λ> length $ myFromJust (encodeWords morseTable stringList)
 112
 λ> (encodeWords morseTable stringList)
 Just [Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Silence,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Silence,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Silence,Beep,Beep,Beep,Silence,Beep,Beep,Beep,Silence,Silence,Silence,Beep,Beep,Beep,Silence,Beep,Silence]
 λ> 

请注意,在这种情况下,mapM id 与库函数 sequence :: Monad m => t (m a) -> m (t a) 相同,后者肯定在 Prelude 集中。

为了全面公开,Prelude 库还包含一个 traverse 函数,它是 sequencemap 之间的调情。所以你可以像这样写一个更优雅的 encodeWords 版本:

encodeWords2 :: Table -> [String] -> Maybe Code
encodeWords2 table stringList =
    let  sil6 = replicate 6 Silence
    in   intercalate' sil6  <$>  traverse (encodeWord table) stringList