haskell

How would I use lens in Haskell to duplicate Python's enumerate?

自古美人都是妖i 提交于 2019-12-30 10:06:31
问题 Python's enumerate on lists can be written as zip [0..] . I looked at Control.Lens.Traversal and Control.Lens.Indexed, but I couldn't figure out how to use lenses to generalize this to any reasonable container (I hesitate to say "Traversable"). I'm guessing itraverse or itraverseOf is key. 回答1: If you're using a container that is an instance of FunctorWithIndex then you can simply use imap (,) : > imap (,) "abc" [(0,'a'),(1,'b'),(2,'c')] But if the index isn't the position this won't work: >

Advantages of using truncation towards minus infinity vs towards zero

☆樱花仙子☆ 提交于 2019-12-30 09:59:32
问题 I was wondering which are the benefits of using truncation towards minus infinity (Haskell, Ruby) instead of truncation towards zero (C, PHP), from the perspective of programming languages/compilers implementation. It seems that truncating towards minus infinity is the right way to go, but I haven’t found a reliable source for such claiming, nor how such decision impact the implementation of compilers. I’m particularly interested in possible compilers optimizations, but not exclusively.

Endofunction as Monoid

北战南征 提交于 2019-12-30 09:56:10
问题 I'm trying this (for learning purposes): {-# LANGUAGE FlexibleInstances #-} instance Monoid (a -> a) where mempty = id mappend f g = f . g expecting id <> id to be equal to id . id However, with (id <> id) 1 I receive this error: Non type-variable argument in the constraint: Monoid (a -> a) What should I change to run it? It's just to understand monoids and Haskell typeclasses better, not for any practical usage . 回答1: This will need {-# OVERLAPPING #-} pragma since GHC.Base has an instance

Can you formulate the Bubble sort as a monoid or semigroup?

大憨熊 提交于 2019-12-30 09:32:33
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr

Can you formulate the Bubble sort as a monoid or semigroup?

一个人想着一个人 提交于 2019-12-30 09:31:48
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr

Type abstraction in GHC Haskell

荒凉一梦 提交于 2019-12-30 09:05:05
问题 I'd love to get the following example to type-check: {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} module Foo where f :: Int -> (forall f. Functor f => Secret f) -> Int f x _ = x g :: (forall f. Functor f => Secret f) -> Int g = f 4 type family Secret (f :: * -> *) :: * where Secret f = Int I get it that it's probably not possible to infer and check g s type (even though in this specific case it's obvious

Why Haskell doesn't accept my combinatoric “zip” definition?

冷暖自知 提交于 2019-12-30 08:27:06
问题 This is the textbook zip function: zip :: [a] -> [a] -> [(a,a)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys I asked on #haskell earlier wether "zip" could be implemented using "foldr" alone, no recursion, no pattern matching. After some thinking, we noticed the recursion could be eliminated using continuations: zip' :: [a] -> [a] -> [(a,a)] zip' = foldr cons nil where cons h t (y:ys) = (h,y) : (t ys) cons h t [] = [] nil = const [] We are still left with pattern matching

Haskell - Export data constructor

泪湿孤枕 提交于 2019-12-30 08:26:11
问题 I have this data on my Module Formula : data Formula = Formula { typeFormula :: String, nbClauses :: Int, nbVars :: Int, clauses :: Clauses } And I want to export it but I don't know the right syntax : module Formula ( Formula ( Formula ), solve ) where Someone can tell me the right syntax please ? 回答1: Some of your confusion is coming from having the same module name as the constructor you're trying to export. module Formula ( Formula ( Formula ), solve ) where Should be module Formula (

Point-free style and using $

早过忘川 提交于 2019-12-30 08:18:16
问题 How does one combine using $ and point-free style? A clear example is the following utility function: times :: Int -> [a] -> [a] times n xs = concat $ replicate n xs Just writing concat $ replicate produces an error, similarly you can't write concat . replicate either because concat expects a value and not a function. So how would you turn the above function into point-free style? 回答1: You can use this combinator: (The colon hints that two arguments follow) (.:) :: (c -> d) -> (a -> b -> c) -

How to install Haskell Platform on Linux Debian Wheezy?

瘦欲@ 提交于 2019-12-30 08:18:09
问题 Initially I thought I would get install Haskell with couple of commands using apt-get but its seems somehow complex. As I look at the haskell org download page, I downloaded haskell-platform-2013.2.0.0.tar.gz . Then next step is somehow confusing. It ask to install GHC before installing platform but at the same time if one opens GHC download page , it shows some warning e.g Stop ! ..... we recommend installing the Haskell Platform instead of GHC . Please guide me how to install Haskell on