Hi, I am writing a custom read function for one of the data types in my module. For eg, when I do read "(1 + 1)" :: Data, I want it to return Plus 1 1. My data declaration is data Data = Plus Int Int. Thanks
In the example given in http://web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/
-- Increase salary by percentage
increase :: Float -> Company -> Company
increase k = everywhere (mkT (incS k))
-- "interesting" code for increase
incS :: Float -> Salary -> Salary
incS k (S s) = S (s * (1+k))
how come increase function compiles without binding anything for the first Company mentioned in its type signature.
Is it something like assigning to a partial function? Why is it done like that?
What is "bleeding edge" these days? has it all been done before us, and we are just discovering new ways of implementing mathematical constructs within programming?
Functional Programming seems to be making inroads in all areas, but is this just marketing to create interest in a programming arena where it appears that the state of the art has climaxed too soon.
have the sales men got hold of the script, and selling ideas that can be sold, dumbing down the future?
I see very old ideas making their way into the market place....what are the truly new things that should be considered fresh and new in 2010 onwards, and not some 1960-1980 idea being refocused.
In your favorite programming language, write a program that, when run, will print out its own source code!
Sounds interesting, now let's go!
ps: Literally, there is "NO" use case for this, just plain curiosity!
I use Ctrl p a lot instead of up arrow to go up on Terminal. How to make ghci support Ctrl p to go up?
I use ghci from ghc98 from port. Mac OS X 10.5.8.
I need to split a list into a list of all possible tuples, but I'm unsure of how to do so.
For example
pairs ["cat","dog","mouse"]
should result in
[("cat","dog"), ("cat","mouse"), ("dog","cat"), ("dog","mouse"), ("mouse","cat"), ("mouse","dog")]
I was able to form the first two, but am unsure of how to get the rest.
Here's what I have so far:
pairs :: [a] -> [(a,a)]
pairs (x:xs) = [(m,n) | m <- [x], n <- xs]
I'm very newbie to Ubuntu OS and I have my solution developed in visual studio 2008.
I want my application to run in both windows and Linux(ubuntu).
I've few questions in my mind.
1)Does mono support visual debugger .
2)If I start development using mono.Is it possible to run same in .net framework (windows) ?? or do I need to write the NSI script to download the libraries during installation from internet and install.
3)what is the best way to achieve platform independence.
Thanks in advance.
I'm looking to improve the D garbage collector by adding some heuristics to avoid garbage collection runs that are unlikely to result in significant freeing. One heuristic I'd like to add is that GC should not be run more than once per X amount of time (maybe once per second or so). To do this I need a timer with the following properties:
It must be able to grab the correct time with minimal overhead. Calling core.stdc.time takes an amount of time roughly equivalent to a small memory allocation, so it's not a good option.
Ideally, should be cross-platform (both OS and CPU), for maintenance simplicity.
Super high resolution isn't terribly important. If the times are accurate to maybe 1/4 of a second, that's good enough.
Must work in a multithreaded/multi-CPU context. The x86 rdtsc instruction won't work.
This is just a hypothetical scenario to illustrate my question. Suppose that there are two threads and one TVar shared between them. In one thread there is an atomically block that reads the TVar and takes 10s to complete. In another thread is an atomically block that modifies the TVar every second. Will the first atomically block ever complete? Surely it will just keep going back to the beginning, because the log is in an inconsistent state?
Hi,
I'm currently doing research to start a new project. This project will be in 2 parts a light Client (probably console) and an heavy one using silverlight. The light client must be cross-platform.
However, they will both use the same Core (by the way, the core will need to use the sockets).
I'd like to use C++ to build the light client but given that the core is common to both applications, it would be much appreciated if it's could be the same code.
So the question is quite simple : Can Silverlight be compilated with a C++ static library ? And if it's possible, what about cross-platform issues (with moonlight) ?
If it's not possible. Which language can i use to work with silverlight while being cross-platform ? Because of performance, a compilated language will be better !
Thanks for your expertise :)
I want to apply a function f to a list of values, however function f might randomly fail (it is in effect making a call out to a service in the cloud).
I thought I'd want to use something like map, but I want to apply the function to all elements in the list and afterwards, I want to know which ones failed and which were successful.
Currently I am wrapping the response objects of the function f with an error pair which I could then effectively unzip afterwards
i.e. something like
g : (a->b) -> a -> [ b, errorBoolean]
f : a-> b
and then to run the code ... map g (xs)
Is there a better way to do this? The other alternative approach was to iterate over the values in the array and then return a pair of arrays, one which listed the successful values and one which listed the failures. To me, this seems to be something that ought to be fairly common. Alternatively I could return some special value. What's the best practice in dealing with this??
I've been trying to figure out how I can make staticText elements resize to fit their contents with wxHaskell. From what I can tell, this is the default behavior in wxWidgets, but the wxHaskell wrapper specifically disables this behavior. However, the library code that creates new elements has me very confused. Can anyone provide an explanation for what this code does?
staticText :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText parent props
= feed2 props 0 $
initialWindow $ \id rect ->
initialText $ \txt -> \props flags ->
do t <- staticTextCreate parent id txt rect flags {- (wxALIGN_LEFT + wxST_NO_AUTORESIZE) -}
set t props
return t
I know that feed2 x y f = f x y, and that the type signature of initialWindow is
initialWindow :: (Id -> Rect -> [Prop (Window w)] -> Style -> a) -> [Prop (Window w)] -> Style -> a
and the signature of initialText is
initialText :: Textual w => (String -> [Prop w] -> a) -> [Prop w] -> a
but I just can't wrap my head around all the lambdas.
I've read this article, but didn't understand last section.
The author says that Monad gives us context sensitivity, but it's possible to achieve the same result using only an Applicative instance:
let maybeAge = (\futureYear birthYear -> if futureYear < birthYear
then yearDiff birthYear futureYear
else yearDiff futureYear birthYear) <$> (readMay futureYearString) <*> (readMay birthYearString)
It's uglier for sure, but beside that I don't see why we need Monad. Can anyone clear this up for me?
data Thing = Thing {a :: Int, b :: Int, c :: Int, (...) , z :: Int} deriving Show
foo = Thing 1 2 3 4 5 (...) 26
mkBar x = x { c = 30 }
main = do print $ mkBar foo
What is copied over when I mutate foo in this way? As opposed to mutating part of a structure directly.
Data Thing = Thing {a :: IORef Int, b :: IORef Int, (...) , z :: IORef Int}
instance Show Thing where
(...something something unsafePerformIO...)
mkFoo = do a <- newIORef 1
(...)
z <- newIORef 26
return Thing a b (...) z
mkBar x = writeIORef (c x) 30
main = do foo <- mkFoo
mkBar foo
print foo
Does compiling with optimizations change this behavior?
I wrote following program:
isPrime x = and [x `mod` i /= 0 | i <- [2 .. truncate (sqrt x)]]
primes = filter isPrime [1 .. ]
it should construct list of prime numbers. But I got this error:
[1 of 1] Compiling Main ( 7/main.hs, interpreted )
7/main.hs:3:16:
Ambiguous type variable `a' in the constraints:
`Floating a' arising from a use of `isPrime' at 7/main.hs:3:16-22
`RealFrac a' arising from a use of `isPrime' at 7/main.hs:3:16-22
`Integral a' arising from a use of `isPrime' at 7/main.hs:3:16-22
Possible cause: the monomorphism restriction applied to the following:
primes :: [a] (bound at 7/main.hs:3:0)
Probable fix: give these definition(s) an explicit type signature
or use -XNoMonomorphismRestriction
Failed, modules loaded: none.
If I specify signature for isPrime function explicitly:
isPrime :: Integer -> Bool
isPrime x = and [x `mod` i /= 0 | i <- [2 .. truncate (sqrt x)]]
I can't even compile isPrime function:
[1 of 1] Compiling Main ( 7/main.hs, interpreted )
7/main.hs:2:45:
No instance for (RealFrac Integer)
arising from a use of `truncate' at 7/main.hs:2:45-61
Possible fix: add an instance declaration for (RealFrac Integer)
In the expression: truncate (sqrt x)
In the expression: [2 .. truncate (sqrt x)]
In a stmt of a list comprehension: i <- [2 .. truncate (sqrt x)]
7/main.hs:2:55:
No instance for (Floating Integer)
arising from a use of `sqrt' at 7/main.hs:2:55-60
Possible fix: add an instance declaration for (Floating Integer)
In the first argument of `truncate', namely `(sqrt x)'
In the expression: truncate (sqrt x)
In the expression: [2 .. truncate (sqrt x)]
Failed, modules loaded: none.
Can you help me understand, why am I getting these errors?
I'm pretty familiar with using Adobe Flex & AS3, and compared with writing apps in JS/HTML I think it's very cool. However, since AIR is essentially a non-browser version of Flex with benefits like local storage, it seems to be competing as a cross-platform desktop application platform... and in that space it's much less mature than more established desktop technologies.
So what's the advantage of creating a desktop application using AIR compared to something like Java (or C++ using a cross-platform GUI library like wxWidgets)? Java's equally capable of communicating with the server for instance, I'm not quite sure what AIR adds when competing head-to-head in the desktop development world?
I pretty much understand 3/4 the rest of the language, but every time I dip my feet into using classes in a meaningful way in my code I get permantently entrenched.
Why doesn't this extremely simple code work?
data Room n = Room n n deriving Show
class HasArea a where
width :: (Num n) => a -> n
instance (Num n) => HasArea (Room n) where
width (Room w h) = w
So, room width is denoted by ints or maybe floats, I don't want to restrict it at this point. Both the class and the instance restrict the n type to Nums, but it still doesn't like it and I get this error:
Couldn't match expected type `n1' against inferred type `n'
`n1' is a rigid type variable bound by
the type signature for `width' at Dungeon.hs:11:16
`n' is a rigid type variable bound by
the instance declaration at Dungeon.hs:13:14
In the expression: w
In the definition of `width': width (Room w h) = w
In the instance declaration for `HasArea (Room n)'
So it tells me the types doesn't match, but it doesn't tell me what types it thinks they are, which would be really helpful. As a side note, is there any easy way to debug an error like this? The only way I know to do it is to randomly change stuff until it works.
how can i do multiple calls to SDL.pollEvent :: IO Event until the output is SDL.NoEvent and collect all the results into a list?
in imperative terms something like this:
events = []
event = SDL.pollEvent;
while( event != SDL.NoEvent )
events.add( event )
event = SDL.pollEvent
How would I print the multiples of a list of given numbers in a merged, sorted list?
I.e.
take 10 (multiples [4,5])
gives
4,5,8,10,12,15,16,20,24,25
I've got it working for lists of size 2 or 1 but I need a more general solution :)
I have declared the following
type KEY = (IPv4, Integer)
type TPSQ = TVar (PSQ.PSQ KEY POSIXTime)
type TMap = TVar (Map.Map KEY [String])
data Qcfg = Qcfg { qthresh :: Int, tdelay :: Rational, cwpsq :: TPSQ, cwmap :: TMap, cw
chan :: TChan String } deriving (Show)
and would like this to be serializable in a sense that Qcfg can either be written to disk or be sent over the network. When I compile this I get the error
No instances for (Show TMap, Show TPSQ, Show (TChan String))
arising from the 'deriving' clause of a data type declaration
Possible fix:
add instance declarations for
(Show TMap, Show TPSQ, Show (TChan String))
or use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Show Qcfg)
I am now not quite sure whether there is a chance at all to serialize my TChan although all individual nodes in it are members of the show class.
For TMap and TPSQ I wonder whether there are ways to show the values in the TVar directly (because it does not get changed, so there should no need to lock it) without having to declare an instance that does a readTVar ?
writeSTRef twice for each iteration
fib3 :: Int -> Integer
fib3 n = runST $ do
a <- newSTRef 1
b <- newSTRef 1
replicateM_ (n-1) $ do
!a' <- readSTRef a
!b' <- readSTRef b
writeSTRef a b'
writeSTRef b $! a'+b'
readSTRef b
writeSTRef once for each iteration
fib4 :: Int -> Integer
fib4 n = runST $ do
a <- newSTRef 1
b <- newSTRef 1
replicateM_ (n-1) $ do
!a' <- readSTRef a
!b' <- readSTRef b
if a' > b'
then writeSTRef b $! a'+b'
else writeSTRef a $! a'+b'
a'' <- readSTRef a
b'' <- readSTRef b
if a'' > b''
then return a''
else return b''
Benchmark, given n = 20000:
benchmarking 20000/fib3
mean: 5.073608 ms, lb 5.071842 ms, ub 5.075466 ms, ci 0.950
std dev: 9.284321 us, lb 8.119454 us, ub 10.78107 us, ci 0.950
benchmarking 20000/fib4
mean: 5.384010 ms, lb 5.381876 ms, ub 5.386099 ms, ci 0.950
std dev: 10.85245 us, lb 9.510215 us, ub 12.65554 us, ci 0.950
fib3 is a bit faster than fib4.