gpt4 book ai didi

haskell - 如何在 Haskell 中实现 fromJust 并迭代字符串列表

转载 作者:行者123 更新时间:2023-12-02 02:32:12 24 4
gpt4 key购买 nike

所以我有这些功能:

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 ,嘟,静音,嘟,嘟,嘟,静音,嘟,静音,嘟,静音,静音,静音,嘟,静音,嘟,嘟,嘟,静音,嘟,静音,嘟,静音,静音,静音,嘟,嘟嘟嘟,静音,嘟嘟,嘟嘟,静音,嘟嘟,嘟嘟,静音]

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

示例:输入:["HI","THERE"] 输出:[Beep,Silence,Beep,Silence,Beep,Silence,Beep,Silence,Silence,Silence,Beep ,静音,嘟嘟,静音,静音,静音,静音,静音,静音,静音,嘟嘟,嘟嘟,嘟嘟,静音,静音,静音,嘟嘟,静音,嘟嘟,静音,嘟嘟,静音,嘟嘟,静音,静音,静音,嘟,静音,静音,静音,嘟,静音,嘟,嘟,嘟,静音,嘟,静音,静音,静音,嘟,静音]

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

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 个 Silence

最佳答案

如果出现问题,则返回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

关于haskell - 如何在 Haskell 中实现 fromJust 并迭代字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64881810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com