For your last question (about reduction to hnf), use the attached code; search the haskell mailing list for "deepseq" for more.
-- Hal Daume III "Computer science is no more about computers | [EMAIL PROTECTED] than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Sun, 27 Jan 2002, Jorge Adriano wrote: > > > Am I the only one who'd like to have some the function specified by > > scan_and_fold f e xs= (scanl f e xs, foldl f e xs) > In the Lists library. Or is it there somewhere and I missed it? > > What about: > > pair (f,g) x = (f x, g x) > > cross (f, g) = pair(f.fst, g.snd) > I kind of like point free style. > > > Any thumb rule for using arrays? I'm expecting access to be O(1), it is right? > Need to have a set of data, and I just want to get random elements from that > Set, arrays seem like a good solution... am I right? > > > DiffArrays... aren't they supposed to be just like Arrays expect for the (//) > behaviour (and accum and accumArray....), If so, why is there no Show > instance for them? Or Functor instance? When I tried to change to DiffArray > it broke everywhere... > Speaking of functors, in the Haskell98 Library Report: > "The two functions map and ixmap derive new arrays from existing ones (...) > The map function transforms the array values while ixmap allows for > transformations on array indices." > That is supposed to be fmap, not map right? > > > Unboxed types... Ghci > > f n = 3# > loads ok and, > Test> :t f > forall t. t -> PrelGHC.Int# > > nice... now this: > > f :: Int -> Int# > > f n = 3# > when trying to load I get: > > Test.hs:3: Type constructor or class not in scope: `Int#' > Failed, modules loaded: none. > What am I missing here? Tried Int#, PrelGHC.Int... nothing worked... > and Ghci also didn't know about (+#) either... > > > > Last but not least, got this algorithm > w_(n+1) = w_(n) + deltaW > > The ws and deltaW are vectors (lists). The deltaW is calculated using w_(n) > and some random point from a DataSet. So I'm doing something like > > lstep :: TrainingSet->WeightVector->WeightVector > lstep ts w = w `vectorsum` deltaW > where > bla, bla > > learn ::TrainSet->WeightVec->WeightVec > learn ts w0 = until p (lstep tSet) w0 > where > bla, bla > > [actually I'm making things a little more simple than they are... lstep > receives and returns a triple with the, w, stdGen to get the random val, n. > of steps...] > > I'm having some stack overflows in my program and I think this is what's > causing it. The suspension in my calculated weight vectors... I should be > evaluating the weight vectors in each step... (agree?) > But how can I evaluate a whole list? seq or $! just reduce elements to head > normal form. > [Yes, I know I can always print them all! :-) > In fact, just to see if that was the cause I tried printing them right now... > Well I'd have to print 50000 results, and I'm short on disk space... > redirected the output to dev null and it is printing for about 40min now > still no stack overflow... ] > > > Also got this problem if I want to check evaluation times. I used to do it > something like this when calculating simple values: > > > time1 <- getCPUTime > > val <- return $! f x > > time2 <- getCPUTime > > But this doesn't work if val is a list of values. So how do you do it? > Wouldn't it be nice to have some strict 'lists'? > Once I asked why is aren't there any operators to reduce elements to their > normal form (not head normal), I was answered that for some types there is no > such concept... Is that because of the newtype declaration? (if it isn't I > think I'm missing something here... functions are in normal form, for > primitive datatypes it's the same as head normal form, and for constructed > datatypes you can define it by structural recursion...) > > > And now I'm off to bed > Thanks to those who actually read the whole mail :-) > J.A. > > _______________________________________________ > Haskell-Cafe mailing list > [EMAIL PROTECTED] > http://www.haskell.org/mailman/listinfo/haskell-cafe >
module DeepSeq where class DeepSeq a where deepSeq :: a -> b -> b deepSeq = seq -- default, for simple cases infixr 0 `deepSeq`, $!! ($!!) :: (DeepSeq a) => (a -> b) -> a -> b f $!! x = x `deepSeq` f x instance DeepSeq () where instance (DeepSeq a) => DeepSeq [a] where deepSeq [] y = y deepSeq (x:xs) y = deepSeq x $ deepSeq xs y instance (DeepSeq a,DeepSeq b) => DeepSeq (a,b) where deepSeq (a,b) y = deepSeq a $ deepSeq b y instance (DeepSeq a,DeepSeq b,DeepSeq c) => DeepSeq (a,b,c) where deepSeq (a,b,c) y = deepSeq a $ deepSeq b $ deepSeq c y instance (DeepSeq a,DeepSeq b,DeepSeq c,DeepSeq d) => DeepSeq (a,b,c,d) where deepSeq (a,b,c,d) y = deepSeq a $ deepSeq b $ deepSeq c $ deepSeq d y instance (DeepSeq a,DeepSeq b,DeepSeq c,DeepSeq d,DeepSeq e) => DeepSeq (a,b,c,d,e) where deepSeq (a,b,c,d,e) y = deepSeq a $ deepSeq b $ deepSeq c $ deepSeq d $ deepSeq e y instance (DeepSeq a,DeepSeq b,DeepSeq c,DeepSeq d,DeepSeq e,DeepSeq f) => DeepSeq (a,b,c,d,e,f) where deepSeq (a,b,c,d,e,f) y = deepSeq a $ deepSeq b $ deepSeq c $ deepSeq d $ deepSeq e $ deepSeq f y instance (DeepSeq a,DeepSeq b,DeepSeq c,DeepSeq d,DeepSeq e,DeepSeq f,DeepSeq g) => DeepSeq (a,b,c,d,e,f,g) where deepSeq (a,b,c,d,e,f,g) y = deepSeq a $ deepSeq b $ deepSeq c $ deepSeq d $ deepSeq e $ deepSeq f $ deepSeq g y instance DeepSeq Bool where instance DeepSeq Char where instance (DeepSeq a) => DeepSeq (Maybe a) where deepSeq Nothing y = y deepSeq (Just x) y = deepSeq x y instance (DeepSeq a, DeepSeq b) => DeepSeq (Either a b) where deepSeq (Left a) y = deepSeq a y deepSeq (Right b) y = deepSeq b y instance DeepSeq Ordering where instance DeepSeq Integer where instance DeepSeq Int where instance DeepSeq Float where instance DeepSeq Double where
