https://wiki.haskell.org/index.php?title=Haskell_Quiz/Goedel/Solution_Dolio&feed=atom&action=historyHaskell Quiz/Goedel/Solution Dolio - Revision history2024-03-28T10:17:06ZRevision history for this page on the wikiMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Haskell_Quiz/Goedel/Solution_Dolio&diff=19088&oldid=prevDolio at 22:51, 11 February 20082008-02-11T22:51:27Z<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 22:51, 11 February 2008</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Haskell Quiz <del class="diffchange diffchange-inline">Solutions</del>|Goedel]]</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Haskell Quiz <ins class="diffchange diffchange-inline">solutions</ins>|Goedel]]</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Encoding is quite simple in Haskell, as it's simply combining the stream of characters with a stream of primes in the right way. Extracting a message from the encoded number is somewhat more cumbersome, especially if one wants to implement the somewhat more efficient factoring discussed in the ruby quiz writeup. However, once such operations are defined for a single number, retrieving a stream is just an unfoldr away.</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Encoding is quite simple in Haskell, as it's simply combining the stream of characters with a stream of primes in the right way. Extracting a message from the encoded number is somewhat more cumbersome, especially if one wants to implement the somewhat more efficient factoring discussed in the ruby quiz writeup. However, once such operations are defined for a single number, retrieving a stream is just an unfoldr away.</div></td>
</tr>
</table>Doliohttps://wiki.haskell.org/index.php?title=Haskell_Quiz/Goedel/Solution_Dolio&diff=19087&oldid=prevDolio at 22:50, 11 February 20082008-02-11T22:50:40Z<p></p>
<p><b>New page</b></p><div>[[Category:Haskell Quiz Solutions|Goedel]]<br />
<br />
Encoding is quite simple in Haskell, as it's simply combining the stream of characters with a stream of primes in the right way. Extracting a message from the encoded number is somewhat more cumbersome, especially if one wants to implement the somewhat more efficient factoring discussed in the ruby quiz writeup. However, once such operations are defined for a single number, retrieving a stream is just an unfoldr away.<br />
<br />
<haskell><br />
module Main (main) where<br />
<br />
import Control.Arrow<br />
import Data.Char<br />
import Data.List<br />
import System.Environment<br />
<br />
primes :: [Integer]<br />
primes = 2 : sieve [3,5..]<br />
where sieve (x:xs) = x : sieve (filter (\n -> n `mod` x /= 0) xs)<br />
<br />
goedel :: String -> Integer<br />
goedel = product . zipWith (^) primes . map ord<br />
<br />
letter :: ([Integer], Integer) -> Maybe (Char, ([Integer], Integer))<br />
letter (_, 1) = Nothing<br />
letter ([], _) = Nothing<br />
letter (p:ps,n) = Just (chr k, (ps, n'))<br />
where (n', k) = extract p n<br />
<br />
extract :: Integer -> Integer -> (Integer, Int)<br />
extract p n = foldl' extract' (n,0) eps<br />
where eps = map (id &&& (p^)) [64,32,16,8,4,2,1]<br />
extract' (n,s) (k,pp)<br />
| m /= 0 = (n,s)<br />
| otherwise = (d,s + k)<br />
where (d,m) = n `divMod` pp<br />
<br />
ungoedel :: Integer -> String<br />
ungoedel = unfoldr letter . (,) primes<br />
<br />
main = do (mode:_) <- getArgs<br />
case mode of<br />
'e':_ -> interact $ show . goedel<br />
'd':_ -> interact $ ungoedel . read<br />
_ -> putStrLn "Unrecognized mode. 'e' for encode, 'd' for decode."<br />
</haskell></div>Dolio