haskell

Walk through a list split function in Haskell

血红的双手。 提交于 2021-02-08 14:11:47
问题 This is a follow up to my previous question. I am trying to understand the list splitting example in Haskell from here: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) I can read Haskell and know what foldr is but don't understand this code. Could you walk me through this code and explain it in more details ? 回答1: Let’s try running this function on a sample input list, say [1,2,3,4,5] : We start with foldr (\a ~(x,y) -> (a:y,x)) ([],[]) [1,2,3,4,5] . Here a is the first element of the list, and (x,y)

Why is `guard` based on `Alternative`?

ε祈祈猫儿з 提交于 2021-02-08 14:11:45
问题 Why is guard based on Alternative ? guard :: Alternative f => Bool -> f () -- guard b is pure () if b is True, -- and empty if b is False. I ask because guard only uses the empty from Alternative . It doesn't use <|> from Alternative at all. So why bother using Alternative in the first place? I guess this is because there is some unstated idea behind Alternative 's empty that matches perfectly with what we're trying to accomplish with guard (stop on False , continue on True ). If this is the

groupBy-like function such that the binary predicate holds between consecutive elements of each group instead of any two

为君一笑 提交于 2021-02-08 13:57:14
问题 On Hackage I see that groupBy's implementation is this: groupBy :: (a -> a -> Bool) -> [a] -> [[a]] groupBy _ [] = [] groupBy eq (x:xs) = (x:ys) : groupBy eq zs where (ys,zs) = span (eq x) xs which means that the preticate eq holds between any two elements of each group . Examples: > difference_eq_1 = ((==1).) . flip (-) > first_isnt_newline = ((/= '\n').) . const > > Data.List.groupBy difference_eq_1 ([1..10] ++ [11,13..21]) [[1,2],[3,4],[5,6],[7,8],[9,10],[11],[13],[15],[17],[19],[21]] > >

Summing over lists of arbitrary levels of nestedness in F#

£可爱£侵袭症+ 提交于 2021-02-08 12:37:13
问题 I'm trying to create an F# function that will return the sum of a list of int s of arbitrary nestedness. Ie. it will work for a list<int> , a list<list<int>> and a list<list<list<list<list<list<int>>>>>> . In Haskell I would write something like: class HasSum a where getSum :: a -> Integer instance HasSum Integer where getSum = id instance HasSum a => HasSum [a] where getSum = sum . map getSum which would let me do: list :: a -> [a] list = replicate 6 nestedList :: [[[[[[[[[[Integer]]]]]]]]]]

About 'pseq' in Haskell

社会主义新天地 提交于 2021-02-08 12:34:08
问题 Consider the following two statements: (a `par` b) `pseq` (a + b) and a `par` (b `pseq` (a + b)) Can someone explain how their behavior differ from each other? For the first one, if the main thread has done with computing b but the spark computing a hasn't finished, will the main thread proceed to compute a + b ? 回答1: par a b is semantically equivalent to b , but it gives the hint that it might be useful to start evaluating a early. On the otherhand pseq forces the evaluation of its first

Haskell: Flaw in the description of applicative functor laws in the hackage Control.Applicative article?: it says Applicative determines Functor

烂漫一生 提交于 2021-02-08 12:30:49
问题 I think I found a flaw in the hackage article for Control.Applicative. As a description of the applicative functor laws, it says: class Functor f => Applicative f where A functor with application, providing operations to embed pure expressions ( pure ), and sequence computations and combine their results ( <*> ). A minimal complete definition must include implementations of these functions satisfying the following laws: identity pure id <*> v = v composition pure (.) <*> u <*> v <*> w = u <*>

How to compose polymorphic functions in Haskell? [duplicate]

半世苍凉 提交于 2021-02-08 11:29:40
问题 This question already has an answer here : What is the monomorphism restriction? (1 answer) Closed last month . I have the following file: module SimpleComposition where class Domain a where f :: a -> a g :: a -> a h = f . g When trying to loading it in ghci, I get the error src\play.hs:7:5: error: * No instance for (Domain c0) arising from a use of `f' * In the first argument of `(.)', namely `f' In the expression: f . g In an equation for `h': h = f . g I believe the problem is that the

How to compose polymorphic functions in Haskell? [duplicate]

◇◆丶佛笑我妖孽 提交于 2021-02-08 11:29:18
问题 This question already has an answer here : What is the monomorphism restriction? (1 answer) Closed last month . I have the following file: module SimpleComposition where class Domain a where f :: a -> a g :: a -> a h = f . g When trying to loading it in ghci, I get the error src\play.hs:7:5: error: * No instance for (Domain c0) arising from a use of `f' * In the first argument of `(.)', namely `f' In the expression: f . g In an equation for `h': h = f . g I believe the problem is that the

Random walk on a pointed container

北慕城南 提交于 2021-02-08 08:19:15
问题 Let us consider a dwarf wandering in a tunnel. I will define a type that represents this situation thusly: data X a = X { xs :: [a], i :: Int } display :: X Bool -> IO () display X{..} = putStrLn (concatMap f xs) where { f True = "*" ; f False = "-" } Here you see a dwarf in a section of a tunnel: λ display x -*--- It is discovered that a pointed container is an instance of Comonad. I can use this instance here to define a function that simulates my dwarf moving right: shiftRight :: X Bool ->

Raycaster displays phantom perpendicular wall faces

有些话、适合烂在心里 提交于 2021-02-08 06:58:19
问题 The output looks like this: You should just see a flat, continuous red wall on one side, blue wall on another, green on another, yellow on another (see the definition of the map, testMapTiles , it's just a map with four walls). Yet there are these phantom wall faces of varying height, which are perpendicular to the real walls. Why? Note that the white "gaps" aren't actually gaps: it's trying to draw a wall of height Infinity (distance 0). If you specifically account for it (this version of