Questions tagged [maybe]
In Haskell, the Maybe type encapsulates an optional value.
308
questions
1
vote
1
answer
64
views
Non-exhaustive patterns in function - Maybe type [duplicate]
In the following function definition I am getting Non-exhaustive patterns in function tapChar.
What am I missing?
reverseTap :: DaPhone -> Char -> [(Digit, Presses)]
reverseTap phone s
| ...
3
votes
1
answer
64
views
How can I handle Nothing branch of maybe monad gracefully?
data Union = F Float | I Int | S String
getUnionFromString :: String -> Union
getUnionFromString str =
case (readMaybe str :: Maybe Int) of
Just i -> I i
Nothing ->
...
5
votes
4
answers
201
views
How to recursively iterate over a list with Maybe objects in Haskell
I am trying to create a function justifyList that takes a list of Maybe objects ([Maybe a]) and return a list of all objects of Just type, discarding any element that is Nothing in the parameter list.
...
0
votes
1
answer
135
views
Finding max of a list of Maybe values in Haskell using Higher Order Functions
I am trying to use higher level order functions to take a list of Maybe values and return a max of type int but for some reason I can only return a Maybe value.
For example this works:
max' iL = foldl ...
0
votes
1
answer
92
views
What is This More General Almost-Traversal Operation
Trying to wrap my mind around traversals - or maybe something slightly different, in this case.
I understand a traversal to be an operation that performs an applicative operation (or effect) over a ...
2
votes
1
answer
233
views
How is the Maybe monad useful in TypeScript?
I’m struggling to understand the value of having a Maybe type in TypeScript. I’m trying to make sense of the code example from this article about implementing your own Maybe type in TypeScript:
enum ...
-1
votes
1
answer
102
views
How to stop Maybe from being passed into another function in Haskell [closed]
I have already looked at this answer, but it is unclear to me how to apply it in my situation.
I have the following function:
designC :: DesignC -> Maybe CDef -> String
designC FirstString _ = &...
-1
votes
4
answers
134
views
Another Maybe for "find minimum" functions
Maybe is useful when we are searching "maximum" value and unconvinient for finding minimum
-- update maximum could be used as "lambda"
updateMaximum :: (Ord a) => Maybe a -> ...
1
vote
1
answer
134
views
Implementing Maybe/Option type in TypeScript using class inheritance
Most of the Typescript implementations of the Maybe/Option type that I have seen either use an interface with a tag to distinguish between the Some/None variants. I have seen a few that don't and ...
0
votes
0
answers
111
views
RxJava: How to filter Optional.empty in a stream of Maybe<Optional<?> without resulting in empty maybe values?
This is what I have so far. This results in empty Maybe values and consequent operations throw MaybeSource is empty exception.
doc.values().stream()
.map(value -> getDoc(value) //returns ...
1
vote
1
answer
113
views
Haskell (Maybe, x, Maybe y, Maybe z) -> Maybe (x,y,z)
I have a set of Haskell IO bound calls that return Maybe Z, Maybe Y, Maybe Z, where X /= Y /= Z.
I would like a function that returns Just (X,Y,Z) where ALL are Just n, or else Nothing.
A naive ...
-1
votes
1
answer
366
views
What is Some in OCaml
This Ocaml code traverses a list and outputs the last element.
I dont understand the second condition where we output Some x
let rec last = function
| [] -> None
| x::[] -> Some x
| _ :: t -&...
0
votes
1
answer
74
views
How can i use map correctly in Haskell
I am trying to make a program, that takes a letter and turns that letter into Morse code.
The Morse table is given, as well as charToCode :: [(Char, String)] -> Char -> String
I also have to use ...
2
votes
2
answers
78
views
Haskell Maybe adder without Arguments
I am wondering if it is possible to wirte the Function
add :: Maybe Int -> Maybe Int
add Just x = Just (x+1)
add Nothing = Nothing
without the x. Similar to
f = Just.(+1)
However
add Just = Just.(...
4
votes
3
answers
250
views
Obtaining a Bool out of [Maybe Bool] which is guaranteed to contain at least one Just
I have an input list of type [Maybe SomeType] and a predicate p of type SomeType -> Bool, and I want to answer the question "Does the predicate p hold for all SomeTypes that happen to be in ...
1
vote
1
answer
87
views
How can I update an element in a tuple list?
I'm new to Haskell and I'm trying to update an element in a tuple list. My tuple list is of type [(String, Int)], I only want to decrement the integer by -1 if the string I get is in the list, ...
2
votes
4
answers
183
views
Why do I have to call `sum` twice to sum a list of `Maybe Integer`s?
I wrote a solution for a very simple practice problem:
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. Write code that shows how many grains are ...
1
vote
1
answer
124
views
The "Maybe" monad and simple "try/catch" solution
I have a question about using the "Maybe" monad in object-oriented languages.
Namely, the "spirit" of using the monad says instead of writing code like:
return getUser(id).group....
1
vote
3
answers
164
views
Why can't you use 'Just' syntax without 'let..in' block in Haskell?
I have a few questions regarding the Just syntax in Haskell.
When question arose when I was experimenting with different ways to write a function to calculate binomial coefficients.
Consider the ...
1
vote
2
answers
138
views
How to deal with a list of 'Maybe [values]' in Haskell?
I tryied to adapt some exercises of the excelent book "Land of Lisp" of Conrad Barski in order to learn Haskell. The idea is to make a simple text game engine.
Concretely I tried :
type Clau ...
7
votes
1
answer
167
views
Reduce nestedness when using successive Either/Maybe
This is probably a very basic Haskell question, but let's assume the following function signatures
-- helper functions
getWeatherInfo :: Day -> IO (Either WeatherException WeatherInfo)
craftQuery ::...
2
votes
2
answers
132
views
Calculate module of "Just int" in Haskell
I'm trying to calculate the module of an index of a list.
list=[5,6,7,8]
a = elemIndex 7 list
b = mod a 2
Ideally, this would give me b = 0 since a = 2 (technically).
But I'm getting error messages ...
0
votes
1
answer
64
views
How can I check which list has the least copies of one element?
I am writing a sudoku Puzzle in Haskell and I have a [[Maybe Int]] where I need to check which of the [Maybe Int] contains the least Nothing elements. In other words, in the following code I should ...
0
votes
1
answer
39
views
Process a Either Reply [Maybe ByteString] into a [Maybe Text]
The following code runs a mget command with Hedis, and return the result as a [Maybe BS.ByteString]:
-- | Text to ByteString
tbs :: Text -> BS.ByteString
tbs = BS.pack . T.unpack
-- | ByteString ...
0
votes
1
answer
29
views
Calling a Function with integer [[mayb_unused]] macro and default argument boolean type parameter
I am writing a Function with 2 Parameters, 1st is integer type marked [[maybe_unused]] and 2nd is Boolean Type with Default Argument false.
int preOrderTraversial([[maybe_unused]] int searchData, bool ...
2
votes
2
answers
77
views
Iterating over a Data.Set until success
Suppose I have a function representing some computation that can fail
f :: a -> Maybe b
If I have a list l, I can find the first (when scanning left-to-right) item in the list on which f succeeds ...
3
votes
1
answer
138
views
Latest non-Nothing values in a list
I have recently started learning Haskell and I hope you can help with a list transformation question.
I have a Maybe list of a custom data type, simplified to Strings for this forum, and I want ...
2
votes
1
answer
148
views
Haskell extract hostname from URLType
I'm using Network.URL and importing a URL from a String:
url_type $ fromJust $ importURL "https://google.com/"
Absolute (Host {protocol = HTTP True, host = "google.com", port = ...
2
votes
1
answer
51
views
How to use higher-order constructs in a particular case
I want to write a function that takes two Maybe Int parameters and returns the minimum of them if they are both Just number, and 'the other' if either of them is Nothing. I'm not satisfied with my ...
0
votes
1
answer
33
views
Better RXJava2 Maybe chaining
I must handle a Maybe in an async chain. In fact I want to send emails via an async mail client (vertx rxjava2 mail client)
The mail may have one attachment or not.
So I wrote a method:
private fun ...
1
vote
1
answer
394
views
Using Just and Nothing Haskell
I am implementing a lambda calculus interpreter and one of the functions I have to write is specified as follows. I have written all of the other functions but this one is really giving me trouble ...
2
votes
2
answers
198
views
How to implement fromJust and iterate over a list of strings in Haskell
So I have got these functions:
intercalate' :: [a] -> [[a]] -> [a]
intercalate' xs xss = concat (intersperse' xs xss)
intersperse' :: a -> [a] -> [a]
intersperse' _ [] =...
1
vote
0
answers
27
views
How to defer failure from inner function to outer
I have a function g : (T -> Double) -> T which expects a function f : T -> Double.
However, I don't have a T -> Double. I have a function f_err : T -> Maybe Double that might fail. I ...
3
votes
1
answer
118
views
How do I use putStrLn on Maybe value in Haskell Stack test?
I am using the Haskell Stack to create a program. My program builds okay but I would like to write a test file so that I can run stack test on my existing program. My problem is that my function to ...
3
votes
3
answers
223
views
Filter over [Maybe a] and discard Nothing values
With a list of Maybe a, how to filter and take only the elements of the list which are not Nothing?
-- input
pas = [Just 3, Just 1, Just 5, Just 9, Nothing, Just 10, Nothing] :: [Maybe Int]
-- ...
0
votes
1
answer
504
views
Haskell Maybe list element operations
I'm learning list operations in Haskell and now I'm trying out various list operations on Maybe list type. Currently, I have this implementation of sum of elements in list in Haskell
sum :: Num a =>...
1
vote
1
answer
712
views
Testing Optional equivalence with FluentAssertions
I'm using a library called Optional (https://github.com/nlkl/Optional) that allows the "maybe" abstraction that is common to functional languages.
The library is awesome, but I'm facing a ...
3
votes
1
answer
398
views
How to use the Agda standard library's typeclass instances, e.g. Maybe's Applicative?
I have experience with type classes in Haskell, interfaces in Idris, and the typeclass pattern in Scala; but I have not figured out how to use Agda's encoding of typeclasses or the typeclasses ...
0
votes
1
answer
135
views
Troubles understanding why maybe type signature in Haskell
Hi I am having troubles understanding the type signature that I need for my function.
-- findPassword :: Map.Map Hash Passwd -> Int -> Hash -> Maybe Passwd
findPassword rTable width hashVal = ...
2
votes
2
answers
373
views
Mapping and filtering a list of maybes with a maybe returning function
I have a list of maybes and a function that gives me a node's color (if present):
maybeNeighbors :: [Maybe Node]
nodeColor :: Node -> Maybe Color
Now I'd like to map colors to nodes, and as ...
10
votes
2
answers
681
views
What should a Traversable instance look like for a Tree datatype with a nested Maybe value?
I have a Haskell exam in three days, so I thought I should practice a little and pulled up past exams, one of which features the following Tree datatype:
data Tree a = Leaf1 a | Leaf2 a a | Node (...
0
votes
1
answer
96
views
How to type this Maybe functor?
This is my attempt to type a Maybe functor (the implementation is an adaptation from Dr. Frisby's):
interface Maybe<T> {
isNothing: () => boolean;
map: <S>(fn: (x: T) => S) =>...
0
votes
2
answers
606
views
How to convert Maybe (List) to Just List in Elm?
I have a list which is comprised of a list of strings, declared as:
type alias Model =
{ board : List (List String)
}
init : Model
init =
{ board =
[ ["", "", ...
2
votes
4
answers
487
views
How do I subtract Maybe values in Haskell? For example, if I want to subtract Just 8 - Just 5 to get Just 3, how would I do that?
How can I do the following. For example, if I want to subtract Just 8 - Just 5 to get Just 3, how would I do that?
Just 8 - Just 5 = Just 3
Just 15 - Just 9 = Just 6
4
votes
3
answers
273
views
How to use fmap on [maybe string] so added string is before Just?
I got following function:
maybeStrings:: [Maybe String] -> [Maybe String]
maybeStrings s = fmap ("Hello, "++) <$> s
The question is, how can I get that "Hello, " before Just?
For example, ...
1
vote
1
answer
79
views
How to avoid lift in MaybeT and why the final validation is not effective?
How can we avoid - if it makes sense to do so.. - the use of lift in MaybeT?
Classical example from wikibooks.
Given
isValid :: String -> Bool
isValid s = length s >= 8
&& ...
1
vote
3
answers
679
views
Haskell avoiding double-wrapped maybes in functions that return maybes
I can see that there's a lot of questions asking about the Maybe type and composition here, but I'm quite stuck and reading these has my head hurting, if I'm being quite honest.
Here's my situation:
...
1
vote
0
answers
158
views
Sanctuary `Maybe` refers to a value, but being used as type here
I have a function like this
export const createMemberId: (x: any) => Maybe<MemberId> = (x: any) => {
try {
return Just({
memberId: mongoose.Types.ObjectId(x),
...
2
votes
2
answers
140
views
Real World Haskell Chapter 3 excercise: Binary Tree with 1 value constructor - follow up
This question is not a duplicate
A question with the same title already exists, but the answer only partially addressed it, in my opinion, and I am interested also in what it left unaswered.
...
2
votes
0
answers
619
views
Lodash FP curry + TypeScript - loosing type information
I'm trying to implement simple Maybe functor in TypeScript:
type Maybe<T> = T | null;
function mapMaybe<T, T2>(fn: (value: T) => T2, maybe: Maybe<T>): Maybe<T2> {
if (...