All Questions
71 questions
3
votes
3
answers
93
views
Haskell basic - pattern matching vs list tail
I'm doing Set7.hs from haskell.mooc.fi/ Exercises7
-- Ex 5: reverse a NonEmpty list.
--
-- PS. The Data.List.NonEmpty type has been imported for you
-- below doesn't work
-- reverseNonEmpty :: ...
1
vote
2
answers
105
views
Implement a cake baking state machine in Haskell
Once again, I cannot wrap my head around this piece of cake:
data Event = AddEggs | AddFlour | AddSugar | Mix | Bake
deriving (Eq, Show)
The cake baking states are as of now:
data State = Start | ...
3
votes
2
answers
231
views
how to converting function to cps recursively
i'd like to define cpsRec as follows but i couldn't.
please let me know if you come up with ideas for implementation.
import Control.Monad.Trans.Cont (Cont)
type family ContRec r x where
ContRec r (...
0
votes
2
answers
161
views
Why does matching this function parameter seprately work, but not with guards. (Haskell)
So, I have been trying to use SDL to make a simple GUI. This is so that I start to understand how to use haskell. In this case, I was using https://github.com/palf/haskell-sdl2-examples/blob/master/...
0
votes
2
answers
138
views
Pattern Matching Element in List in Haskell
Let's say I have the following type, where KType is defined somewhere else:
data PExpr =
PVal KType |
PCall String [PExpr]
I have a function:
tryReplace:: PExpr -> Maybe PExpr
That for ...
2
votes
1
answer
106
views
Haskell - Function not working with infinite lists [duplicate]
I wrote a function which cuts a list into half in every possible way.
My problem is that I used the 'lenght' function so it can't work with infinite lists.
I couldn't think of anything that actually ...
2
votes
4
answers
298
views
Why isn't (20 >) . length . take 10 === const True
tl;dr
Isn't the fact that 20 < length $ take 10 $ whatever requires whatever to successfully pattern patch a list (at least either [] or (_:_)) a "lack" of laziness?
Or, in other words, ...
1
vote
1
answer
116
views
Function expects Char in place of [Char]
I am new to Haskell and functional programming, and can't understand why this function cannot identify the correct type:
mformat :: [Char] -> [Char] -> [Char]
mformat first last = ((formatted ...
1
vote
1
answer
452
views
How can I use a type annotation to specify a variable in Haskell?
I am running test cases for functions I created in Haskell. I created a function called zip3Lists. It takes in any type (polymorphic arguments) of three lists and returns list of 3-tuples where each 3-...
1
vote
1
answer
353
views
pattern matching of negative literals in Haskell
I was trying to understand why n + k patterns were banned in Haskell. A famous post on StackOverflow gives an example of a function as follows:
f 0 = 0
f (n + 5) = 5
Haskell shows an error while ...
-1
votes
1
answer
74
views
Haskell - A problem with non-exhaustive patterns in function
I have the following haskell code:
Why doesn't x0's pattern matching to function f?
1
vote
2
answers
216
views
Haskell - Number of occurencies of a Char in a String [duplicate]
I am writing a function in Haskell to count the occurencies of a Char value in a String, but getting an error with the pattern matching. Is there any way I could change the pattern matching to check ...
-1
votes
1
answer
71
views
Generic Pattern in Haskell
I was hoping that Haskell's compiler would understand that f v Is type-safe given Unfold v f (Although that is a tall order).
data Sequence a = FirstThen a (Sequence a) | Repeating a | UnFold b (b -&...
3
votes
1
answer
233
views
Transpose nested data structures
I need a function like Vector [Vector a] -> Vector [Vector a], where everything stays within the same list-index, but (for each list-index) the i-th element of the j-th inner-vector becomes the j-...
1
vote
1
answer
199
views
Is there a way to use "<=" for pattern matching in Haskell?
I have the following code, that drops every nth element in a list.
dropEvery :: [a] -> Int -> [a]
dropEvery xs n = f xs n ++ dropEvery (drop n xs) n
where
f ys 0 = []
f ys 1 =...