Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Haskell

Name: Anonymous 2006-11-04 7:02

ghc -fglasgow-exts -lcrypt trip.hs
% ./a.out faggot
Ep8pui8Vw2


fap fap fap

module Main where
    import Data.Char
    import Foreign.C.String
    import System.Environment
    import System.IO.Unsafe

    foreign import ccall "crypt.h crypt" c_crypt :: CString -> CString -> IO CString

    crypt :: String -> String -> String
    crypt key salt =
        taketail 10 $
            unsafePerformIO $ peekCString $
            unsafePerformIO $
                withCString key $ \k ->
                withCString salt $ \s ->
                    c_crypt k s
        where
        taketail :: Int -> [a] -> [a]
        taketail i x = (reverse (take i (reverse x)))

    makesalt :: String -> String
    makesalt x =
        map
            fixsalt
            (take 2
                (if (length x) < 2 then ("H.") else (tail x)))
        where
        fixsalt :: Char -> Char
        fixsalt c
            | (inrange '0' '9' c)  = c
            | (inrange ':' '@' c)  = (chr ((ord c) + 7))
            | (inrange 'A' 'Z' c)  = c
            | (inrange '[' '`' c)  = (chr ((ord c) + 6))
            | (inrange 'a' 'z' c)  = c
            | c == '/' || c == '.' = c
            | otherwise            = '.'
            where
            inrange :: Ord a => a -> a -> a -> Bool
            inrange low upp x = x >= low && x <= upp

    tripcode :: String -> String
    tripcode s = crypt s (makesalt s)

    main :: IO ()
    main = do
        args <- getArgs
        putStrLn (tripcode (head args))

Name: Anonymous 2006-12-09 14:14

Okay guys, I just learned list comprehension, and using that, I managed to turn my five line permutation function into a one-line golden masterpiece (I can't even understand the code anymore!).  How's this for concise (LOL)!!

permutate :: Eq a => [a] -> [[a]]
permutate [] = [[]]
permutate xs = [y : ys | y <- xs, ys <- (permutate (filter (/= y) xs))]


Any ideas on how I can remove the Eq dependency (/=) elegantly?  I realize you usually want to be able to distinguish elements in a permutation, but it'd still be nice.  Hmm... thinking about the code, I just figured it out!  It does ys <- for every y <-.  Nice, that explains why it works :)

Now, I want to see you C fanboys do this in 2000 lines of code!!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List