1) Write a subroutine that accepts an array of unsigned ints and returns 4. It must operate on the array in-place and partition it so that all nonzero values are at the beginning of the array, and all zero values are moved to the end. For example, the input [0, 2, 0, 0, 4, 1, 4, 5] could be changed to [2, 4, 1, 4, 5, 0, 0, 0]. The relative order of the nonzero values is unimportant.
inplace :: [Word] -> [Word] inplace xs = runST $ do vec <- unsafeThaw . fromList $ xs let len = length vec one i pos | i < len = do val <- read vec i if val /= 0 then do write vec pos val one (i+1) (pos+1) else one (i+1) pos | otherwise = return pos two j | j < len = do write vec j 0 two (j+1) | otherwise = return () one 0 0 >>= two res <- unsafeFreeze vec return . toList $ res
Name:
Anonymous2014-11-25 4:01
All the computer people are drinking the cult coolaid of more bureaucracy. I don't understand the insanity. They vehemently attack and despise me. I'm look at these dumb fucks want the dick of pointless bureaucracy stuffed in their mouth. Fuck Git. Fuck typechecking. Fuck all that homo shit.
(define (vector-remove! fn fill v) (define (fill-from i) (if (< i (vector-length v)) (begin (vector-set! v i fill) (fill-from (+ i 1))))) (let iter ((i 0) (j 0)) (if (< i (vector-length v)) (let ((c (fn (vector-ref v i)))) (if (not c) (vector-set! v j (vector-ref v i))) (iter (+ i 1) (+ j (if c 0 1)))) (fill-from j))))
(define (retoid-funcy-wuncy-wunctor v) (vector-remove! zero? v 0) 4)
Name:
L. A. Calculus!jYCj6s4P.g2014-11-25 21:12
s/(vector-remove! zero? v 0)/(vector-remove! zero? 0 v)/
Name:
L. A. Calculus!jYCj6s4P.g2014-11-25 21:16
N WAT KINDA FUCKIN RETOID (IM LOOKIN AT U, SUSSMAN) DESIGNS A LANGUAGE DAT DOESN'T ALLOW U TO REDUCE DA SIZE OF A FUCKIN VECTOR?
My understanding is that the values are unboxed as the Vector is created by calling fromList, but I may be wrong about this. I guess I could avoid reboxing the result and just return a Vector Word.
Name:
Anonymous2014-11-26 0:40
>>31 You're supposed to be modifying an existing array and returning 4.
I swear, the Lisp hackers are the only people in this thread who know how to read.
>>37 Brevity, and the use of idiomatic and meaningful instructions, is a lot more important than the speed hit you'll incur from using loop on modern processors. What are you going to do when someone is reading your ASM code in 20 years and can't untangle your jumble of seemingly unrelated side effects? Please consider the needs of others and don't just do things to make yourself look cool.
Name:
Anonymous2014-11-26 18:53
>>38 Don't worry, we will still all be on /prog/ 20 years hence to explain it to the passing /g/ros anyway. Hell, maybe even Nikita will survive the gulag and will be back here too.
Name:
Anonymous2014-11-26 19:10
>>35 Fuck, if ASM is so succinct and clear, why isn't it used for general-purpose programming? Oh, wait, there was already a thread about this recently.