[Haskell-cafe] [Pipes] Can pipes solve this problem? How?
Hello Cafe. Consider code, that takes input from handle until special substring matched: matchInf a res s | a `isPrefixOf` s = reverse res matchInf a res (c:cs) = matchInf a (c:res) cs hTakeWhileNotFound str hdl = hGetContents hdl = return.matchInf str [] It is simple, but the handle is closed after running. That is not good, because I want to reuse this function. Code can be rewritten without hGetContent, but it is much less comprehensible: hTakeWhileNotFound str hdl = fmap reverse$ findStr str hdl [0] [] where findStr str hdl indeces acc = do c - hGetChar hdl let newIndeces = [ i+1 | i - indeces, i length str, str!!i == c] if length str `elem` newIndeces then return (c : acc) else findStr str hdl (0 : newIndeces) (c : acc) So, the question is - can pipes (any package of them) be the Holy Grail in this situation, to both keep simple code and better deal with handles (do not close them specifically)? How? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Cannot System,Timeout.timeout Network.accept
Hello Cafe. I cannot accept socket connection with timeout (Windows, GHC 7.4,1). Consider following code: import Network import System.Timeout main = withSocketsDo $ do print :: Starting server ... sock - listenOn (PortNumber 4010) res - timeout 1 (accept sock) print Timed out -- Never output! sClose sock Documentation on http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-Timeout.html says:... Standard I/O functions like ... Network.Socket.accept, ... appear to be blocking, but they really don't because the runtime system uses scheduling mechanisms like select(2) to perform asynchronous I/O, so it is possible to interrupt standard socket I/O or file I/O using this combinator. Neither Network.accept, nor Network.Socket.accept can be timed out is example above. Is it bug? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How helpful is h-99 (and should we complete the missing ones)?
So here's my question: how useful is h-99 (are they overrated as learning tools)? I find myself solve most of them in a from the scratch fashion (e.g., no Monad, no Applicative, no Functor aside from List and a few Maybe). Some of them are paper-worthy, for example the prime problems. I hope some guru-level Haskeller could do away the missing few, and maybe dive deeper into the surface to produce more insights (like the knights travel page or the sieve paper, which are both beautiful). As another new-haskeller I say: Yes No. Yes for necessity of several described algorithms and gained intuition for real programming. No for needed ammount of work to understand pitfalls of Haskell when real programming. And of-course, problems and solutions are not annotated with there typical real world aplications, they are not obvious for average beginners. Why would I make Binary tree balanced, when I don't know, what I'll gain (except balanced binary tree)? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [C][enums][newbie] What is natural Haskell representation of such enum?
Thanks. This and previous email are answers to question I asked. But not the answer to question I mean. I'll describe the whole task, as Yves Parès suggested. I'm trying to convert C++ code to Haskell. I have such hierarchy: class Object, class Item : Object, class Container : Item. Another one example: class Unit : Object, class Player : Unit. Each constructor do things like this: Object::Object() { objectType= TYPEMASK_OBJECT; // ... lots of code ... } Item::Item() { objectType |= TYPEMASK_ITEM; // ... } Container::Container(): Item() { objectType |= (TYPEMASK_ITEM | TYPEMASK_CONTAINER); // ... } What is objectType? This field is used when a networksend packet is created. In the packet it is 1 byte of flags, so it is in object hierarchy. So the question was: what type should objectType field have in Haskell? I think it must not mimic enum. What the structure have I to use? There is one more problem - there may be lots of objects, lots of, so memory efficiency is also suggested. And side question: what to do with classes? =) Maybe there is simple rule to convert OO hierarchy to FP. 23 січня 2012 р. 12:15 Malcolm Wallace malcolm.wall...@me.com написав: 2012/1/22 Данило Глинський abcz2.upr...@gmail.com What is natural Haskell representation of such enum? enum TypeMask { UNIT, GAMEOBJECT, CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT }; I don't think that definition makes any sense in C, because UNIT is 0, so UNIT | GAMEOBJECT == GAMEOBJECT == 1 Nevertheless, in Haskell something vaguely similar might be: data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT // 1-byte flaged enum enum TypeMask { // ... UNIT= 0x0004, GAMEOBJECT = 0x0008, // ... CREATURE_OR_GAMEOBJECT = UNIT | GAMEOBJECT WORLDOBJECT = UNIT | PLAYER | GAMEOBJECT | DYNAMICOBJECT | CORPSE // ... even more enum combos ... }; import Data.Bits data TypeMask = UNIT | GAMEOBJECT | CREATURE_OR_GAMEOBJECT | WORLDOBJECT instance Enum TypeMask where fromEnum UNIT = 0x4 fromEnum GAMEOBJECT = 0x8 fromEnum CREATURE_OR_GAMEOBJECT = fromEnum UNIT .|. fromEnum GAMEOBJECT fromEnum WORLDOBJECT = fromEnum UNIT .|. fromEnum PLAYER .|. fromEnum GAMEOBJECT .|. fromEnum DYNAMICOBJECT .|. fromEnum CORPSE toEnum 0x4 = UNIT toEnum 0x8 = GAMEOBJECT toEnum _ = error unspecified enumeration value of type TypeMask isCreatureOrGameObject :: Int - Bool isCreatureOrGameObject x = (x .|. fromEnum CREATURE_OR_GAMEOBJECT) /= 0 isWorldObject :: Int - Bool isWorldObject x = (x .|. fromEnum WORLDOBJECT) /= 0 -- But fundamentally, this is not an idiomatic Haskell way of doing things. -- The other posts in this thread have shown more Haskell-ish translations. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe