haskell

Type constraints for automatic function constraint deduction in Haskell

老子叫甜甜 提交于 2021-02-07 13:13:52
问题 For educational purposes I am playing around with trees in Haskell. I have Tree a type defined like this data Tree a = EmptyTree | Node a (Tree a) (Tree a) and a lot of functions that share a basic constraint - Ord a - so they have types like treeInsert :: Ord a => a -> Tree a -> Tree a treeMake :: Ord a => [a] -> Tree a and so on. I can also define Tree a like this data Ord a => Tree a = EmptyTree | Node a (Tree a) (Tree a) but I can not simplify my functions and omit the extra Ord a to be

haskell sorting

ぃ、小莉子 提交于 2021-02-07 12:11:17
问题 How can it be done in most simply way to write (or maybe there is something embedded in haskell) function which takse as arguments list of tuples (String, Int) and Int x and return top x tuples as list according to x value. I wonder if its possible to write a function which also takes 3 argument which is the name of (or index) of filed in tuple according to which sorting has to be done. What are best solutions to make it quite generic? 回答1: take x $ sortBy (compare `on` fst) [("asd", 1), ...]

haskell sorting

喜夏-厌秋 提交于 2021-02-07 12:01:09
问题 How can it be done in most simply way to write (or maybe there is something embedded in haskell) function which takse as arguments list of tuples (String, Int) and Int x and return top x tuples as list according to x value. I wonder if its possible to write a function which also takes 3 argument which is the name of (or index) of filed in tuple according to which sorting has to be done. What are best solutions to make it quite generic? 回答1: take x $ sortBy (compare `on` fst) [("asd", 1), ...]

Why does my HUnit test suite fail but pass successfully in Cabal?

荒凉一梦 提交于 2021-02-07 11:54:21
问题 If I have test/Test.hs with module Main where import Test.HUnit test1 :: Test test1 = TestCase $ assertEqual "Should be one" 1 5 test2 :: Test test2 = TestCase $ assertEqual "Shold both be zero" 0 0 main :: IO Counts main = runTestTT $ TestList [test1, test2, test1] and a .cabal with test-suite my-test type: exitcode-stdio-1.0 hs-source-dirs: test main-is: Test.hs build-depends: base >= 4.8.1.0 && <4.9, HUnit >= 1.3 default-language: Haskell2010 and I run cabal test --show-details='always'

How can I optimize parallel sorting to improve temporal performance?

亡梦爱人 提交于 2021-02-07 11:54:08
问题 I have an algorithm for parallel sorting a list of a given length: import Control.Parallel (par, pseq) import Data.Time.Clock (diffUTCTime, getCurrentTime) import System.Environment (getArgs) import System.Random (StdGen, getStdGen, randoms) parSort :: (Ord a) => [a] -> [a] parSort (x:xs) = force greater `par` (force lesser `pseq` (lesser ++ x:greater)) where lesser = parSort [y | y <- xs, y < x] greater = parSort [y | y <- xs, y >= x] parSort _ = [] sort :: (Ord a) => [a] -> [a] sort (x:xs)

Why does my HUnit test suite fail but pass successfully in Cabal?

拟墨画扇 提交于 2021-02-07 11:54:06
问题 If I have test/Test.hs with module Main where import Test.HUnit test1 :: Test test1 = TestCase $ assertEqual "Should be one" 1 5 test2 :: Test test2 = TestCase $ assertEqual "Shold both be zero" 0 0 main :: IO Counts main = runTestTT $ TestList [test1, test2, test1] and a .cabal with test-suite my-test type: exitcode-stdio-1.0 hs-source-dirs: test main-is: Test.hs build-depends: base >= 4.8.1.0 && <4.9, HUnit >= 1.3 default-language: Haskell2010 and I run cabal test --show-details='always'

Is State monad needed/useful in a language with mutable (local) variables (such as Scala)?

怎甘沉沦 提交于 2021-02-07 09:10:55
问题 I understand that in Haskell the State monad is useful because there are no mutable variables (unless we are in the IO monad). However, what is the deal with Scala ? Is State Monad useful in a language where there are mutable variables ? In some sense all the State Monad allows is to use some local mutable variables within the Monad context. For example here: newtype Labeled anytype = Labeled (S -> (S, anytype)) instance Monad Labeled where return contents = Labeled (\st -> (st, contents))

Inserting into a list at a specific location using lenses

梦想与她 提交于 2021-02-07 06:42:57
问题 I'm trying to perform a manipulation of a nested data structure containing lists of elements. After mucking around with various approaches I finally settled on lenses as the best way to go about doing this. They work perfectly for finding and modifying specific elements of the structure, but so far I'm stumped on how to add new elements. From what I've read, I can't technically use a Traversal as it violates the Traversal laws to insert a new element into a list, and that's assuming I could

Use relative paths for extra-lib-dirs on cabal

China☆狼群 提交于 2021-02-07 06:00:07
问题 I have a C library "myboo" which has Makefile. I want to make a wrapper of this library. I don't want to install it into /usr/local since "myboo" is not a major module. Additionally it is recommended that I build "myboo" not as a dynamic library but as a static library. I make custom Setup.py to build "myboo"; main :: IO () main = defaultMainWithHooks simpleUserHooks { preBuild = \a b -> makeLib a b >> preBuild simpleUserHooks a b } makeLib :: Args -> BuildFlags -> IO () makeLib _ flags = do

Impose nesting limits on recursive data structure

守給你的承諾、 提交于 2021-02-07 05:37:27
问题 Consider a recursive data structure like the following: data Tree level = Leaf String | Node level [ Tree level ] Now, if level is an instance of Ord , I would like to impose at the type level the following limitation on the data structure: a node must contain only Tree s with a higher level . You can safely assume that level is a simple sum type like Level = Level1 | Level2 ... | LevelN but where N is not known a priori. In this case I would be able to have that all the subnodes of a node