haskell

How can I write a MST algorithm (Prim or Kruskal) in Haskell?

霸气de小男生 提交于 2020-01-22 15:35:27
问题 I can write both Prim's and Kruskal's algorithms to find a minimum spanning tree in C++ or Java, but I want to know how to implement them in Haskell with O(mlogm) or O(mlogn) (pure functional programs is better). Thanks a lot. 回答1: As svenningsson suggests, priority search queue is well suited for both Kruskal's and Prim's (atleast the author proclaims it in his paper.) The problem with Kruskal is that it requires that you have an O(log n) union-find algorithm. A union-find datastructure with

Breaking out of monad sequence

白昼怎懂夜的黑 提交于 2020-01-22 15:25:11
问题 Is it possible to break out of a monad sequence? For instance, if I want to break out of a sequence earlier based on some condition calculated in the middle of the sequence. Say, in a 'do' notation I bind a value and based on the value I want to either finish the sequence or stop it. Is there something like a 'pass' function? Thanks. 回答1: Directly using if You could do this directly as Ingo beautifully encapsulated, or equivalently for example breakOut :: a -> m (Either MyErrorType

Closed type families and strange function types

点点圈 提交于 2020-01-22 14:27:20
问题 Sorry, I couldn't imagine any better title for the question, so please read ahead. Imagine that we have a closed type family that maps every type to it's corresponding Maybe except maybes themselves: type family Family x where Family (Maybe x) = Maybe x Family x = Maybe x We can even declare a function using that type family: doMagic :: a -> Family a doMagic = undefined exampleA = doMagic $ Just () exampleB = doMagic $ () Playing with it in GHCi shows that it is ok to evaluate the type of

List comprehension: making lists of lists

泪湿孤枕 提交于 2020-01-22 14:07:56
问题 hi im trying to make a function in haskell that takes a number a makes a partion of it using lists i.e. for number 4 it would create [[1,1,1,1],[1,1,2],[1,3],[2,2],[4]] . I was thinking of using list comprehension for this where it would create list x and then create further lists using numbers from [1...n] (n being the partition number I would want) where the sum of the list created would be equal to n. The code I have created so far is- partions (n:xs) = [[x|x<-[1...n], sum[x]==n]]|xs<-[1..

List comprehension: making lists of lists

淺唱寂寞╮ 提交于 2020-01-22 14:07:05
问题 hi im trying to make a function in haskell that takes a number a makes a partion of it using lists i.e. for number 4 it would create [[1,1,1,1],[1,1,2],[1,3],[2,2],[4]] . I was thinking of using list comprehension for this where it would create list x and then create further lists using numbers from [1...n] (n being the partition number I would want) where the sum of the list created would be equal to n. The code I have created so far is- partions (n:xs) = [[x|x<-[1...n], sum[x]==n]]|xs<-[1..

Currying out of order in Haskell

南笙酒味 提交于 2020-01-22 13:35:07
问题 Is there an elegant notation for Currying the arguments of a function out of order in Haskell? For example, if you wish to divide 2 by all elements of a list, you can write map ((/) 2) [1,2,3,4,5] However to divide all elements of a list it seems you need to define an anonymous function map (\x -> x/2) [1,2,3,4,5] Anonymous functions quickly become unwieldy in more complex cases. I'm aware that in this case map ((*) 0.5) [1,2,3,4,5] would work fine, but I'm interested to know if Haskell has a

How to force main thread to wait for all its child threads finish in Haskell

倖福魔咒の 提交于 2020-01-22 12:00:07
问题 In the following Haskell code, how to force main thread to wait till all its child threads finish. I could not able to use forkFinally as given in the section "Terminating the Program" here in this link: (http://hackage.haskell.org/package/base-4.7.0.2/docs/Control-Concurrent.html). I get desired result when using TMVar. But I want to do this with TVar. Please help. module Main where import Control.Monad import Control.Concurrent import Control.Concurrent.STM type TInt = TVar Int transTest ::

How to force main thread to wait for all its child threads finish in Haskell

白昼怎懂夜的黑 提交于 2020-01-22 11:59:46
问题 In the following Haskell code, how to force main thread to wait till all its child threads finish. I could not able to use forkFinally as given in the section "Terminating the Program" here in this link: (http://hackage.haskell.org/package/base-4.7.0.2/docs/Control-Concurrent.html). I get desired result when using TMVar. But I want to do this with TVar. Please help. module Main where import Control.Monad import Control.Concurrent import Control.Concurrent.STM type TInt = TVar Int transTest ::

If you can't change a variable's value in Haskell, how do you create data structures?

拜拜、爱过 提交于 2020-01-22 05:50:05
问题 As per the title. I have the following code which creates a binary search tree, but if I want it created and changed dynamically with user input, how would I do that if I can't change the value of a variable in haskell?!? find :: (Ord a) => Node a -> a -> Bool find (Node val left right) s | s == val = True | s < val = find left s | s > val = find right s find Empty s = False data Node a = Node a (Node a) (Node a) | Empty myTree = Node "m" (Node "a" Empty Empty) (Node "z" Empty Empty) Thanks

Haskell - need to define Vector2 instance for typeclass

≡放荡痞女 提交于 2020-01-22 04:11:47
问题 newtype Vector2 a = Vector2 (a,a) deriving (Show,Eq) class VectorSpace v where vZero :: (Num a) => v a vSum :: (Num a) => v a -> v a -> v a vScalarProd :: (Num a) => a -> v a -> v a vMagnitude :: (Floating a) => v a -> a Need to define for Vector2 to be instances of the type class VectorSpace. 回答1: So here is what I tried so far: instance VectorSpace (a,a) => VectorSpace Vector2 a vecZero = (0.0,0.0) vecSum (x,y) (x',y') = (x+x',y+y') The first problem here is syntax. You need a where at the