haskell

Hayoo/Hoogle not matching monomorphic/less polymorphic signatures with polymorphic ones

自闭症网瘾萝莉.ら 提交于 2020-05-11 05:14:07
问题 I'm trying to understand if there's a fundamental reason for why Hoogle and Hayoo aren't matching, for instance (b -> c) -> (b' -> c') -> (b, b') -> (c, c') to a b c -> a b' c' -> a (b, b') (c, c') which happens to be the type of Control.Arrow.***: (***) :: a b c -> a b' c' -> a (b, b') (c, c') — is this likely just something that's as-of-yet unimplemented, or is there something fundamental that is preventing this sort of type based search from ever being implemented? 回答1: With Hoogle, it

Hayoo/Hoogle not matching monomorphic/less polymorphic signatures with polymorphic ones

六月ゝ 毕业季﹏ 提交于 2020-05-11 05:11:46
问题 I'm trying to understand if there's a fundamental reason for why Hoogle and Hayoo aren't matching, for instance (b -> c) -> (b' -> c') -> (b, b') -> (c, c') to a b c -> a b' c' -> a (b, b') (c, c') which happens to be the type of Control.Arrow.***: (***) :: a b c -> a b' c' -> a (b, b') (c, c') — is this likely just something that's as-of-yet unimplemented, or is there something fundamental that is preventing this sort of type based search from ever being implemented? 回答1: With Hoogle, it

Haskell: converting a list of (a, b) key-value pairs (with possibly repeated keys) to a list of (a, [b]) grouped by key

你离开我真会死。 提交于 2020-05-10 07:20:40
问题 I'm a Haskell beginner. Let's suppose I want to write a function convertKVList that takes a flat list of key-value pairs, where some of the keys might be repeated, and turns it into a mapping from keys to lists of values where all of the keys are unique. For instance, on a list of pairs of Int s, I want this behavior: > convertKVList [(1, 2), (1, 4), (1, 3), (2, 3)] [(1,[3,4,2]),(2,[3])] This seems like a common enough task that there ought to be a library function available to do what I want

Haskell: how to detect “lazy memory leaks”

霸气de小男生 提交于 2020-05-10 03:26:27
问题 After few hours of debugging, I realized that a very simple toy example was not efficient due to a missing ! in an expression return $ 1 + x (thanks duplode!... but how come ghc does not optimize that??). I also realized it because I was comparing it with a Python code that was quicker, but I won't always write Python code to benchmark my code... So here is my question: is there a way to automatically detect these "lazy memory leaks", that slow down a program for no real reason? I'm still

Are typeclasses essential?

☆樱花仙子☆ 提交于 2020-05-09 18:58:09
问题 I once asked a question on haskell beginners, whether to use data/newtype or a typeclass. In my particular case it turned out that no typeclass was required. Additionally Tom Ellis gave me a brilliant advice, what to do when in doubt: The simplest way of answering this which is mostly correct is: use data I know that typeclasses can make a few things a bit prettier, but not much AFIK. It also strikes me that typeclasses are mostly used for brain stem stuff, wheras in newer stuff, new

What is the kind of Void?

自古美人都是妖i 提交于 2020-05-09 18:39:27
问题 Seeing as the type of Void is uninhabited, can it be regarded as a type "constructor"? Or is this just a quick "hack" to be able to safely disregard / disable functionality and am I looking too deep into this? 回答1: 0 was once not considered to be a number. "How can nothing be something?" But over time we came to accept 0 as a number, noticing its properties and its usefulness. Today the idea that 0 is not a number is as absurd as the idea that it was one 2,000 years ago. Void is a type the

composing two comparison functions?

落爺英雄遲暮 提交于 2020-05-09 18:39:12
问题 I'd like to sort by one property and then by another (if the first property is the same.) What's the idiomatic way in Haskell of composing two comparison functions, i.e. a function used with sortBy ? Given f :: Ord a => a -> a -> Ordering g :: Ord a => a -> a -> Ordering composing f and g would yield: h x y = case v of EQ -> g x y otherwise -> v where v = f x y 回答1: vitus points out the very cool instance of Monoid for Ordering . If you combine it with the instance instance Monoid b => Monoid

Guards vs. if-then-else vs. cases in Haskell

守給你的承諾、 提交于 2020-05-09 17:32:15
问题 I have three functions that find the nth element of a list: nthElement :: [a] -> Int -> Maybe a nthElement [] a = Nothing nthElement (x:xs) a | a <= 0 = Nothing | a == 1 = Just x | a > 1 = nthElement xs (a-1) nthElementIf :: [a] -> Int -> Maybe a nthElementIf [] a = Nothing nthElementIf (x:xs) a = if a <= 1 then if a <= 0 then Nothing else Just x -- a == 1 else nthElementIf xs (a-1) nthElementCases :: [a] -> Int -> Maybe a nthElementCases [] a = Nothing nthElementCases (x:xs) a = case a <= 0

Stack can't find a downloaded module (Data.MultiSet)

不羁的心 提交于 2020-05-09 08:01:17
问题 I've been using stack (in my linux computer) to compile my haskell code and today I installed for the first time an external module. I installed Data.MultiSet by running the following command: sudo stack install multiset Supposedly the module was succesfully installed but I can't load it, stack throws the following message when I try to compile my code: ..error: Could not find module ‘Data.MultiSet’... I checked the files contained in my .stack directory and there are many files with the name

how to print intermediate result in Functor and Applicative in Haskell

北城余情 提交于 2020-05-08 18:59:17
问题 I am reading the book Programming in Haskell 2nd by Graham Hutton. https://www.cs.nott.ac.uk/~pszgmh/pih.html#slides When it comes to chapter 13.4 Sequencing parsers, it contains the following functions: > parse three "abcdef" [((’a’,’c’),"def")] > parse three "ab" [] I would like to understand what are the intermediate steps to evaluate them behind the scene. You can find the working source code for the Functor and Applicative for the Parser here: import Control.Applicative import Data.Char