Head/tail functions expecting int and erroring on [int]


Head/tail functions expecting int and erroring on [int]

I wrote this garbled mess, which is supposed to take two sorted lists and return a merged sorted list:

mergeLists list1 list2 = if head list1 > head list2 then [head [list2]++mergeLists (list1) (tail list2)] else if head list1 < head list2 then [head [list1]++mergeLists (tail list1) (list2)] else [head [list2]++head [list1]++mergeLists (tail list1) (tail list2)] 

Im fairly sure the logic checks out, by building a new list from taking the smallest head value it should always work. There is no edge case for when one list is empty yet, but there will be once I figure out these errors.

My problem is the 3 errors all basically the same, complaining that whenever i pass an int it wants an [int] and whenever i pass an [int] it wants an int. Its driving me mad. It seems to be based on the head/tail calls because when i add extra brackets to make something like head [list1] the errors change. It may be to do with the ++ though.

I do not know Haskell, or functional programming, very well at all. Can someone please point out what i have done wrong?

Submitted February 23, 2018 at 08:07PM by Jamma003
via reddit http://ift.tt/2BMsub9

Advertisements

STM on Free monads!


STM on Free monads!

Hi everyone, Alexander Granin here,

I'm glad to present you a really simple implementation of Software Transactional Memory using Free monads!

Source: stm-free

Currently, my library is PoC, but it works quite well. Source code contains the Dining philosophers task solved with it.

The main STML Free monadic language has 3 basic operations to work with TVar and also Retry. As a quick solution, TVar can contain only a serializable value, but this will be relaxed in the next versions.

newTVar :: ToJSON a => a -> Free STMF (TVar a) writeTVar :: ToJSON a => TVar a -> a -> Free STMF () readTVar :: FromJSON a => TVar a -> Free STMF a retry :: Free STMF () 

Consider this example:

it "TVar created in separate transaction" $ do ctx <- newContext tvar <- atomically ctx $ newTVar (10 :: Int) res <- atomically ctx $ readTVar tvar res `shouldBe` 10 

Interestingly, my atomically combinator has different signature that the original one:

atomically :: Context -> STML a -> IO a 

Context denotes a separate STM runtime for your scenario. TVars created within a context, are not visible from other, so accessing a foreign tvar will give exception. This is not that good, and in the future, I'd probably want to restrict tvars from being used in foreign contexts.

Still, the code can have lots of nonoptimalities, like these:

  • Free monad from the free package has some performance issues;

  • Storing, checking and merging of TVars can be done more effective;

  • ToJSON / FromJSON instances for contained values;

  • Others.

And it's really interesting to measure my library performance and compare it with STM from base. I'll be doing this in the future, but today I have another goal: to port this implementation to C++ for my upcoming talk to the conference C++ Russia.

For now, enjoy. Free monads are really powerful!

Submitted February 23, 2018 at 01:44AM by graninas
via reddit http://ift.tt/2BJgDuj

Obtain lowest power of two with a given leading digit


Obtain lowest power of two with a given leading digit

I have to define lowestPower such that

 lowestPower :: Integer -> (Integer,Integer) 

lowestPower n is the pair (k, m) where m is the lowest power of 2 whose decimal representation begins with the digit n and k is its exponent (i. e. 2k = m). For example,

 lowestPower 3 == (5,32) lowestPower 7 == (46,70368744177664) fst (lowestPower 982) == 3973 fst (lowestPower 4148780) == a 

I have tried this, but it's a bad definition, because for cases greater than 5, it takes a long time and the computer gets blocked. And, uh, I need you to be able to calculate the a

 lowestPower :: Integer -> (Integer,Integer) lowestPower n = head [ (k,m) | k <- [0..] , let m = 2^k, (head . cifras) m == n] cifras :: Integer -> [Integer] cifras n = [read[d] | d <- show n] 

Thanks.

Submitted February 22, 2018 at 08:54PM by AprendiendoHaskell
via reddit http://ift.tt/2CbXLVP