haskell

QuickCheck: Arbitrary instances of nested data structures that generate balanced specimens

泄露秘密 提交于 2019-12-28 12:17:35
问题 tl;dr: how do you write instances of Arbitrary that don't explode if your data type allows for way too much nesting? And how would you guarantee these instances produce truly random specimens of your data structure? I want to generate random tree structures, then test certain properties of these structures after I've mangled them with my library code. (NB: I'm writing an implementation of a subtyping algorithm, i.e. given a hierarchy of types, is type A a subtype of type B. This can be made

Automatic memoizing in functional programming languages

假如想象 提交于 2019-12-28 12:00:20
问题 I always thought that Haskell would do some sort of automatic intelligent memoizing. E.g., the naive Fibonacci implementation fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) would be fast because of that. Now I read this and it seems I was wrong -- Haskell doesn't seem to do automatic memoization. Or do I understand something wrong? Are there other languages which do automatic (i.e. implicit, not explicit) memoization? What are common ways to implement memoization? In all sample

Can someone explain to me why the app function of ArrowApply makes them as powerful as monads?

送分小仙女□ 提交于 2019-12-28 11:11:24
问题 So I'll break my question into 4 parts, but first some background: I feel relatively comfortable with Monads, but not very comfortable with Arrows. I suppose the main problem I have with them is, I don't see what they are useful for. Whether formally correct or not, I understand Monads to be a tool that allows us to introduce side effects from computation. As they generalize program fragments from pure values to values boxed with other actions. From my shotgun "read all the papers" approach

What is predicativity?

爷,独闯天下 提交于 2019-12-28 08:08:22
问题 I have pretty decent intuition about types Haskell prohibits as "impredicative": namely ones where a forall appears in an argument to a type constructor other than -> . But just what is predicativity? What makes it important? How does it relate to the word "predicate"? 回答1: Let me just add a point regarding the "etymology" issue, since the other answer by @DanielWagner covers much of the technical ground. A predicate on something like a is a -> Bool . Now a predicate logic is one that can in

Parsec vs Yacc/Bison/Antlr: Why and when to use Parsec?

╄→尐↘猪︶ㄣ 提交于 2019-12-28 07:58:39
问题 I'm new to Haskell and Parsec. After reading Chapter 16 Using Parsec of Real World Haskell, a question appeared in my mind: Why and when is Parsec better than other parser generators like Yacc/Bison/Antlr? My understanding is that Parsec creates a nice DSL of writing parsers and Haskell makes it very easy and expressive. But parsing is such a standard/popular technology that deserves its own language, which outputs to multiple target languages. So when shall we use Parsec instead of, say,

How to implement Haskell's splitEvery in Swift?

北城以北 提交于 2019-12-28 07:01:11
问题 PROBLEM let x = (0..<10).splitEvery( 3 ) XCTAssertEqual( x, [(0...2),(3...5),(6...8),(9)], "implementation broken" ) COMMENTS I am running into problems calculating number of elements in the Range, etc... extension Range { func splitEvery( nInEach: Int ) -> [Range] { let n = self.endIndex - self.startIndex // ERROR - cannot invoke '-' with an argument list of type (T,T) } } 回答1: The values in a range are of ForwardIndexType , so you can only advance() them, or compute the distance() , but the

Why is there no Show instance for functions?

浪子不回头ぞ 提交于 2019-12-28 06:58:47
问题 Just a quick conceptual question, I am currently trying to learn and understand Haskell better. I know the Show function is used to convert values to strings, but why can't function types be used with show? Prelude> (\x -> x*3) <interactive>:7:1: No instance for (Show (a0 -> a0)) arising from a use of `print' Possible fix: add an instance declaration for (Show (a0 -> a0)) In a stmt of an interactive GHCi command: print it Prelude> 回答1: It's not that they can't, but that there's not usually a

Why is there no Show instance for functions?

本小妞迷上赌 提交于 2019-12-28 06:58:05
问题 Just a quick conceptual question, I am currently trying to learn and understand Haskell better. I know the Show function is used to convert values to strings, but why can't function types be used with show? Prelude> (\x -> x*3) <interactive>:7:1: No instance for (Show (a0 -> a0)) arising from a use of `print' Possible fix: add an instance declaration for (Show (a0 -> a0)) In a stmt of an interactive GHCi command: print it Prelude> 回答1: It's not that they can't, but that there's not usually a

Why does the 2-tuple Functor instance only apply the function to the second element?

北战南征 提交于 2019-12-28 05:57:22
问题 import Control.Applicative main = print $ fmap (*2) (1,2) produces (1,4) . I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple. Update I've basically figured this out almost straight away. I'll post my own answer in a minute.. 回答1: Let me answer this with a question: Which output do you expect for: main = print $ fmap (*2) ("funny",2) You can have something as you want (using data Pair a = Pair a a or so), but as (,) may have

How do you compute the difference between successive elements of a list of unknown size, functionally?

你。 提交于 2019-12-28 05:49:05
问题 In a programming language that is purely functional (like Haskell) or where you are only using it in a functional way (eg clojure); suppose you have a list/seq/enumerable (of unknown size) of integers and you want to produce a new list/seq/enumerable that contains the differences between successive items, how would you do it? What I did previously in C# was to fold over the list and keep a state object as the aggregating value which recorded the 'previous' item so that you could do a diff on