Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 字符串数字到字符串字表示法_Haskell - Fatal编程技术网

Haskell 字符串数字到字符串字表示法

Haskell 字符串数字到字符串字表示法,haskell,Haskell,我正在做一个小练习,帮我做一个更大的练习。现在,我正在尝试编写一个名为“digitStr”的函数,它的签名是(我认为)digitStr::String->String 我在这里试图做的是,当我键入以下内容时,我应该得到以下输出: > digitStr "23" "twenty three" 现在,假设数字只上升到25 我自己试过的: 我的第一个想法是向数据库中添加数字单词 wrdtxt :: txt wrdtxt = " one"," two"," three"," four"," fi

我正在做一个小练习,帮我做一个更大的练习。现在,我正在尝试编写一个名为“digitStr”的函数,它的签名是(我认为)
digitStr::String->String

我在这里试图做的是,当我键入以下内容时,我应该得到以下输出:

> digitStr "23"
"twenty three"
现在,假设数字只上升到25

我自己试过的:

我的第一个想法是向数据库中添加数字单词

wrdtxt :: txt
wrdtxt = " one"," two"," three"," four"," five","six"," seven"," eight"," nine"," ten"," eleven"," twelve","thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen", " twenty"
下一步是创建一个函数,该函数接受字符串并将其输出到字符串列表中:

numWords :: String -> [String]
numWords "" = []
numWords x = words x
下一步是计算函数的长度。如果它的长度是2,我们看第一个数字来确定它是10秒还是20秒。如果是“25”,不知何故,做“20”+“5”


如你所知,我对该做什么有点困惑。在过去的两天里,我一直在考虑这个问题,这就是我能想到的

以下是模式匹配字符串的方法:

numWords :: String -> [String]
numWords []  =  ...    -- matches only the empty string
numWords [a] =  ...    -- matches when argument is just  a single char;
                          a = the digit
numWords [a,b] = ...   -- matches when there are exactly two chars;
                          a = first chars, b = second char
numWords ['1',b] = ... -- matches when there are exactly two characters and
                          the first char is '1'; b is set to the second char
numWords (a:as) = ...  -- matches when there is at least one character;
                          a = first character, as = second through last characters

但您可能还需要考虑更改NUMITS的签名以获取int:

numWords :: Int -> [String]
numWords x
  | x < 10  = ...     -- x is a single digit
  | x < 20  = ...     -- x is between 11 and 19
  | x < 30  = ...     -- x is between 20 and 29
  | ...
numWords::Int->[String]
纽姆沃兹x
|x<10=…--x是一个位数
|x<20=…--x在11到19之间
|x<30=…--x在20到29之间
| ...

您已经有算法了吗?每个任务解决方案都首先考虑算法(这是必须的)。然后,您只需编写代码(这通常很简单)。