haskell

Haskell Fibonacci Explanation

寵の児 提交于 2019-12-29 07:33:26
问题 I am quite new to Haskell and I'm trying to wrap my head around how the lazy expression of Fibonacci sequences work. I know this has been asked before, but none of the answers have addressed an issue I'm having with visualising the result. The code is the canonical one using zipWith fibs = 0 : 1 : zipWith (+) fibs (tail fibs) I understand the following: zipWith literally zips two lists together tail grabs all but the first element of a list Haskell references 'to-be' computed data as thunks .

Could not deduce KnownNat in two existentials with respect to the singletons library

徘徊边缘 提交于 2019-12-29 07:33:10
问题 I was experimenting with the singletons library and I found a case that I don't understand. {-# LANGUAGE GADTs, StandaloneDeriving, RankNTypes, ScopedTypeVariables, FlexibleInstances, KindSignatures, DataKinds, StandaloneDeriving #-} import Data.Singletons.Prelude import Data.Singletons.TypeLits data Foo (a :: Nat) where Foo :: Foo a deriving Show data Thing where Thing :: KnownNat a => Foo a -> Thing deriving instance Show Thing afoo1 :: Foo 1 afoo1 = Foo afoo2 :: Foo 2 afoo2 = Foo athing ::

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

两盒软妹~` 提交于 2019-12-29 07:28:08
问题 With the question Listing all the contents of a directory by breadth-first order results in low efficiencyI learned that the low efficiency is due to a strange behavior of the recursive monad functions. Try sequence $ map return [1..]::[[Int]] sequence $ map return [1..]::Maybe [Int] and ghci will fall into an endless calculation. If we rewrite the sequence function in a more readable form like follows: sequence' [] = return [] sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)} and

How to [temporarily] suppress “defined but not used” warnings?

旧城冷巷雨未停 提交于 2019-12-29 07:22:39
问题 When I prototype Haskell programs, I always get hundreds of warnings like this (not joking): /Users/bob/SourceCode/course/is/expriment/LiftedSpine2.hs:70:15: Warning: Defined but not used: `ta' /Users/bob/SourceCode/course/is/expriment/LiftedSpine2.hs:72:15: Warning: Defined but not used: `ta' /Users/bob/SourceCode/course/is/expriment/LiftedSpine2.hs:77:26: Warning: Defined but not used: `v' Is there anyway to remove these warnings temporarily? I tried putting this in my .hs file: {-# OPTIONS

How to fix “Illegal datatype context” (use -XDatatypeContexts)?

≯℡__Kan透↙ 提交于 2019-12-29 07:17:17
问题 I am a new learner of Haskell, my code is as follows: data Num a=>Units a = Units a (SymbolicManip a ) deriving (Eq) I am not sure how to fix it? Anyone can help me? 回答1: Typeclass contexts in datatypes are now regarded as a not so useful feature. The problem is that the following does not compile: foo :: Units a -> a foo (Units x _) = x+x This intuitively should compile, since the Units a argument can only be constructed for a type a satisfying Num a . So, on destruction (pattern matching)

How are variable names chosen in type signatures inferred by GHC?

久未见 提交于 2019-12-29 07:00:48
问题 When I play with checking types of functions in Haskell with :t , for example like those in my previous question, I tend to get results such as: Eq a => a -> [a] -> Bool (Ord a, Num a, Ord a1, Num a1) => a -> a1 -> a (Num t2, Num t1, Num t, Enum t2, Enum t1, Enum t) => [(t, t1, t2)] It seems that this is not such a trivial question - how does the Haskell interpreter pick literals to symbolize typeclasses? When would it choose a rather than t ? When would it choose a1 rather than b ? Is it

Breaking Data.Set integrity without GeneralizedNewtypeDeriving

爱⌒轻易说出口 提交于 2019-12-29 06:37:07
问题 The code below uses an unsafe GeneralizedNewtypeDeriving extension to break Data.Set by inserting different elements with different Ord instances: {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Data.Set import System.Random class AlaInt i where fromIntSet :: Set Integer -> Set i toIntSet :: Set i -> Set Integer instance AlaInt Integer where fromIntSet = id toIntSet = id newtype I = I Integer deriving (Eq, Show, AlaInt) instance Ord I where compare (I n1) (I n2) = compare n2 n1 -- sic!

Why are GHC Sparks Fizzling?

a 夏天 提交于 2019-12-29 06:22:50
问题 I have a simple routine that takes the product of a vector of Double . I am attempting to parallelize this code, but many of the sparks end up fizzling. Here is a self-contained benchmark which is also provided as a gist: {-# LANGUAGE BangPatterns #-} {-# LANGUAGE MagicHash #-} {-# OPTIONS_GHC -O2 -Wall -threaded -fforce-recomp #-} import Criterion.Main import Control.Monad (when) import Control.Parallel.Strategies (runEval,rpar,rseq) import qualified Data.Vector.Primitive as PV main :: IO ()

Is there an Iteratee-like concept which pulls data from multiple sources?

心已入冬 提交于 2019-12-29 06:13:27
问题 It is possible to pull on demand from a number (say two for simplicity) of sources using streams (lazy lists). Iteratees can be used to process data coming from a single source. Is there an Iteratee-like functional concept for processing multiple input sources? I could imagine an Iteratee whose state signals from which source does it want to pull. 回答1: To do this using pipes you nest the Pipe monad transformer within itself, once for each producer you wish to interact with. For example:

Performance problem with Euler problem and recursion on Int64 types

旧街凉风 提交于 2019-12-29 05:59:10
问题 I'm currently learning Haskell using the project Euler problems as my playground. I was astound by how slow my Haskell programs turned out to be compared to similar programs written in other languages. I'm wondering if I've forseen something, or if this is the kind of performance penalties one has to expect when using Haskell. The following program in inspired by Problem 331, but I've changed it before posting so I don't spoil anything for other people. It computes the arc length of a