haskell

Why is this Haskell expression so slow?

时光怂恿深爱的人放手 提交于 2020-01-21 01:46:26
问题 I'm working on Project Euler Problem 14. Here's my solution. import Data.List collatzLength :: Int->Int collatzLength 1 = 1 collatzLength n | odd n = 1 + collatzLength (3 * n + 1) | even n = 1 + collatzLength (n `quot` 2) maxTuple :: (Int, Int)->(Int, Int)->Ordering maxTuple (x1, x2) (y1, y2) | x1 > y1 = GT | x1 < y1 = LT | otherwise = EQ I'm running the following out of GHCi maximumBy maxTuple [(collatzLength x, x) | x <- [1..1000000]] I know that if Haskell evaluated strictly, the time on

Combining StateT IO with State

喜欢而已 提交于 2020-01-21 01:38:05
问题 If I have a function f :: State Int () , is it possible to use it within another function g :: StateT Int IO () ? Nesting it with f = do { something; g } fails to typecheck with Couldn't match type 'Data.Functor.Identity.Identity' with 'IO' . 回答1: Yes, this operation is usually called "hoisting". For the State monad, it could be defined as hoistState :: Monad m => State s a -> StateT s m a hoistState = state . runState Unfortunately, it is not defined in the Control.Monad.State module. 来源:

Haskell linear algebra?

人盡茶涼 提交于 2020-01-20 20:06:12
问题 I am starting to test Haskell for linear algebra. Does anyone have any recommendations for the best package for this purpose? Any other good resources for doing basic matrix manipulation with Haskell? The haskell wiki lists several resources for this. My current focus in on hmatrix and bindings-gsl, both of which look promising. 回答1: The hmatrix and hmatrix-static libraries are excellent. Hunt around on Hackage some more: http://hackage.haskell.org/package/vect 来源: https://stackoverflow.com

Haskell linear algebra?

独自空忆成欢 提交于 2020-01-20 20:05:59
问题 I am starting to test Haskell for linear algebra. Does anyone have any recommendations for the best package for this purpose? Any other good resources for doing basic matrix manipulation with Haskell? The haskell wiki lists several resources for this. My current focus in on hmatrix and bindings-gsl, both of which look promising. 回答1: The hmatrix and hmatrix-static libraries are excellent. Hunt around on Hackage some more: http://hackage.haskell.org/package/vect 来源: https://stackoverflow.com

confused about function as instance of Functor in haskell

試著忘記壹切 提交于 2020-01-20 02:17:06
问题 the type of fmap in Functor is: fmap :: Functor f => (a -> b) -> f a -> f b it looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f b using Maybe a for example : fmap show (Just 1) result is : Just "1" same as saying: Just (show 1) but when (->) is used as a Functor (in Control.Monad.Instances) import Control.Monad.Instances (fmap show Just) 1 result is : "Just 1" that is, Just is apply first, then show is applied.

confused about function as instance of Functor in haskell

孤街醉人 提交于 2020-01-20 02:15:53
问题 the type of fmap in Functor is: fmap :: Functor f => (a -> b) -> f a -> f b it looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f b using Maybe a for example : fmap show (Just 1) result is : Just "1" same as saying: Just (show 1) but when (->) is used as a Functor (in Control.Monad.Instances) import Control.Monad.Instances (fmap show Just) 1 result is : "Just 1" that is, Just is apply first, then show is applied.

Is there a built-in function to get all consecutive subsequences of size n of a list in Haskell?

牧云@^-^@ 提交于 2020-01-19 01:44:08
问题 For example, I need a function: gather :: Int -> [a] -> [[a]] gather n list = ??? where gather 3 "Hello!" == ["Hel","ell","llo","ol!"] . I have a working implementation: gather :: Int-> [a] -> [[a]] gather n list = unfoldr (\x -> if fst x + n > length (snd x) then Nothing else Just (take n (drop (fst x) (snd x)), (fst x + 1, snd x))) (0, list) but I am wondering if there is something already built into the language for this? I scanned Data.List but didn't see anything. 回答1: You could use

How to extract a list of fields from a list of a given data type?

删除回忆录丶 提交于 2020-01-17 13:26:29
问题 If I have defined a datatype with, let's say, 5 attributes. How would I be able to create a list of one of the attributes. Ex : data Person = Person name fname sexe age height Marie, John, Jessie :: Person Marie = Person "Marie" _ _ _ John = Person "John" _ _ _ Jessie = Person "Jessie" _ _ _ How can I return a list containing all the names : ( Marie , John , Jessie ) 回答1: Your code isn't valid Haskell. You could have data Person = Person FName LName Sexe Age Height type FName = String type

Haskell Happy parser error mismatching types and infinite type

给你一囗甜甜゛ 提交于 2020-01-17 07:20:49
问题 Writing a Oberon-like language parser, I'm having troubles compiling the parser after I've updated it to be able to define more procedures on the same level, and not only nested one inside the other. This is my lexer: { module Lexer where } %wrapper "basic" $alpha = [a-zA-Z] $digit = [0-9] $validChar = [^\"] tokens :- $white+ ; "PROCEDURE" { \s -> KW_TokenProcedure } "END" { \s -> KW_TokenEnd } ";" { \s -> KW_TokenSemiColon } $alpha [$alpha $digit \_]* { \s -> TokenVariableIdentifier s } { --

Generic solution to (Eq, Show) overlapping instances issue when defining class (* -> *)

送分小仙女□ 提交于 2020-01-17 06:41:28
问题 Stack has many threads on overlapping instances, and while these are helpful in explaining the source of the problem, I am still not clear as to how to redesign my code for the problem to go away. While I will certain invest more time and effort in going through the details of existing answers, I will post here the general pattern which I have identified as creating the problem, in the hope that a simple and generic answer exists: I typically find myself defining a class such as: {-# LANGUAGE