Four is the number of letters in the ...: Difference between revisions

→‎{{header|Haskell}}: added Haskell solution
m (added wording to the task)
(→‎{{header|Haskell}}: added Haskell solution)
Line 575:
Word 10000000 is "thousand", with 8 letters. Length of sentence so far: 70995756
</pre>
 
=={{header|Haskell}}==
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.
 
<lang haskell>sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
where
start = "Four is the number of letters in the first word of this sentence, "
add (i, w) = unwords [spellInteger (alphaLength w), "in the", spellOrdinal i ++ ", "]
 
alphaLength w = fromIntegral $ length $ filter isAlpha w
 
main = mapM_ (putStrLn . say) [1000,10000,100000,1000000]
where
ws = words sentence
say n =
let (a, w:_) = splitAt (n-1) ws
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
show (length $ unwords a) ++ " chars."</lang>
 
<pre>λ> take 200 sentence
"Four is the number of letters in the first word of this sentence, two in the second, three in the third, six in the fourth, two in the fifth, seven in the sixth, two in the seventh, three in the eight"
 
λ> main
The one thousandth word is "in" which has two letters. The sentence length is 6349 chars.
The ten thousandth word is "in" which has two letters. The sentence length is 66051 chars.
The one hundred thousandth word is "one" which has three letters. The sentence length is 683690 chars.
The one millionth word is "the" which has three letters. The sentence length is 7349567 chars.</pre>
 
=={{header|Java}}==
Anonymous user