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

Pages: 1-

Haskell Help

Name: Anonymous 2009-06-23 21:22

Okay, major help needed if someone out there has the time, this has been killing my brain for ages.

-- e.g., insertions 'c' "ab" = ["cab", "acb", "abc"]
-- It may help to define two functions which call each other.

Thus far I have.

insertions :: x -> [x] -> [[x]]
insertions x [] = [[]]
insertions x (y:ys) = ??

Name: Anonymous 2009-06-23 21:32

'C' + "AB" = ABCAB

Think about it:


ABCAB ABCAB ABCBA
ABC    BCA    CBA

Name: Anonymous 2009-06-23 21:38

So, you want to insert 'c' into 'ab' at all possible positions, and return a list of those?


import Data.List (splitAt)

insertions :: a -> [a] -> [[a]]
insertions _ [] = []
insertions a as =
  let asrep = replicate (length as + 1) as
      zipped = zip [0..] asrep
      insert (n, l) = let (f, s) = splitAt n l in f ++ [a] ++ s
      inserted = map insert zipped
  in inserted

Name: Anonymous 2009-06-23 21:53

insertions c = uncurry (zipWith $ flip (++).(c:)).(tails &&& inits)

Name: Anonymous 2009-06-23 22:04

insertions :: x -> [x] -> [[x]]
insertions x [] = [[x]]
insertions x (y:ys) = (x:y:ys):map (y:) (insertions x ys)

Name: Anonymous 2009-06-23 22:05

>>5
Pardon me.
insertions :: x -> [x] -> [[x]]
insertions x [] = [[x]]
insertions x (y:ys) = (x:y:ys):map (y:) (insertions x ys)

Name: Anonymous 2009-06-23 22:22

>>6

Perfect, I have no idea what my lecturer was talking about when he said we'd need to implement two other functions. Thanks.

Name: Anonymous 2009-06-23 22:53

>>4
Yours seems pretty slow. Surprisingly, >>6 doesn't seem too bad, even though it's very recursive. In fact, >>6 is faster than mine (>>3).


import Control.Arrow
import Data.List
import Data.Time

insertions :: a -> [a] -> [[a]]
insertions _ [] = []
insertions a as =
  map (\(n, l) -> let (f, s) = splitAt n l in f ++ [a] ++ s) $
  zip [0..] $ replicate (length as + 1) as

arrowInsertions c = uncurry (zipWith $ flip (++) . (c:)) . (tails &&& inits)

naiveInsertions :: x -> [x] -> [[x]]
naiveInsertions x [] = [[x]]
naiveInsertions x (y:ys) = (x:y:ys):map (y:) (insertions x ys)

test :: String -> [[a]] -> IO ()
test name list = do
  start <- getCurrentTime
  let !eval = sum (map length list)
  end <- getCurrentTime
  let diff = diffUTCTime end start
  putStrLn $ name ++ ": took " ++ show diff ++ " seconds"

main = do
  let list = [1..20000]
  let f = insertions 1 list
  let fa = arrowInsertions 1 list
  let fn = naiveInsertions 1 list
  test "mine" f
  test "arrows" fa
  test "naive" fn



$ ghc --make insertions -O3
[1 of 1] Compiling Main             ( insertions.hs, insertions.o )
Linking insertions ...
$ ./insertions
mine: took 28.078299s seconds
arrows: took 84.96184s seconds
naive: took 26.349743s seconds
$

Name: Anonymous 2009-06-24 1:20

holy shit brainwashhed haskell fags do homework for you on /prog/.
What the hell happened to "DONT HELP HIM!!"

Name: Anonymous 2009-06-24 1:41

>>9
Haskell came.

Name: Anonymous 2009-06-24 1:42

>>9
Haskell fags need to feel good about themselves.

Name: Anonymous 2009-06-24 2:42

>>9
Haskell homework threads are more annoying than just Haskell threads, but they're infinitely preferable to Sepples homework threads.

Name: Anonymous 2009-06-24 7:39

>>12
fo reals

Name: Anonymous 2009-06-24 9:41

insertions :: x -> [x] -> [[x]]
insertions x [] = [[]]
insertions x (y:ys) = (y:ys).map(function(s:String, i:int, array:Array) {
  return x + s;
})

Name: Anonymous 2009-06-24 14:33

>>14
WHAT THE FUCK IS THIS HERESY?

Name: Anonymous 2009-06-24 14:48

>>13
Sepples uses float and/or double instead of real.

Name: Anonymous 2009-06-24 17:11

>>16
Interesting piece of trivia:
see/sepples use double by default; one must explicitely use the `f' suffix on a floating-point literal if they want 32-bit floats.

Name: Anonymous 2009-06-24 18:26

>>15
Hasktionscript

Name: Anonymous 2009-06-25 6:35

manual labor can be refreshing and wholesome

Name: Anonymous 2009-06-25 10:59

>>4
You know, the non-pointless version is actually shorter than that.

Name: Anonymous 2010-11-02 14:03

Name: Anonymous 2010-11-14 9:59


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