Questions tagged [guard-clause]

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

See also:

References

64 questions
2
votes
2 answers

Insert an element into a list, to the given indexes, in Haskell

The function has to be like this: insertElemAt :: a -> [Int] -> [a] -> [a]. Examples: insertElemAt 0 [2,5,9] [1..10] = [1, 0, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 10] insertElemAt 0 [1,2,4,8] [0,1,0,0,1,1,0,1] = [0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0,…
Vivi
  • 75
  • 7
2
votes
1 answer

Beginner Haskell problem - Couldn't match type ‘Bool’ with ‘[Char]’

I need to make a palindrome checker using recursion in Haskell for a homework assignment. The function should take in a string and return a Bool. When attempting to compile, I get the error, "Couldn't match type ‘Bool’ with ‘[Char]’." I'm very new…
MC3D
  • 23
  • 2
2
votes
2 answers

Correct indentation rules using guards

I've looked at questions regarding indentation, which were of no help. My indentation also looks correct but according to the compiler it isnt. What is the correct indentation and what are the rules? readFile filename = do …
peterxz
  • 864
  • 1
  • 6
  • 23
2
votes
1 answer

Patterns not matched: (_:_:_)

So I'm trying to build a function that takes a list of tuples and finds the tuple with the biggest second element. But I'm getting a pattern match error. This is my code. resultTuple :: [((Int,Int),Int)] -> (Int,Int) resultTuple [] =…
F.Lachlan
  • 37
  • 6
2
votes
2 answers

Introspect functions with guard clauses

Given a module having two functions with the same arity, but different guard clauses, how do I (ideally) see what those clauses are, or at least that there are two functions? defmodule Test do def greet(name) when name == "foo" do …
AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
2
votes
1 answer

F# reading and separating text from a file

I have a txt file with numbers and comma separated lines like this for example. 4324,1dd3,444 4324,1fd3,444 4324,1as3,442 I have a function that takes a string as a parameter, and I want to check for each line if that string is equal to the second…
jlodenius
  • 829
  • 2
  • 13
  • 24
2
votes
1 answer

Is a when guard the only way to identify if items in a tuple are the same value in a pattern match?

Consider the following function: let private actionPixel(pixelColour:Color) = match (pixelColour.A, pixelColour.R, pixelColour.G, pixelColour.B) with | (0uy, _, _, _) -> transparent | (alpha, red, green, blue) when red = blue &&…
David Arno
  • 42,717
  • 16
  • 86
  • 131
2
votes
2 answers

Pattern combining type test and literal

The active pattern in this question fails to compile after upgrading to VS 2012 RTM. It provides a way to do a type test and match a literal within a single pattern. For example: let (|Value|_|) value = match box value with | :? 'T as x -> Some…
Daniel
  • 47,404
  • 11
  • 101
  • 179
1
vote
1 answer

Haskell Patterns not matched: (_:_) _

I'm writing a function that takes an input a list, creates sublists, and retrieves n elements that it outputs into a new list. I'm writing guards depending on the value of the inputs, but I keep getting the error "Patterns not matched: (:)…
JackOA
  • 25
  • 2
1
vote
1 answer

How to type an input function as a type guard?

How can I generalize a function mapping an array of T1 to a array containing only T2 where T2 is a subtype of T1? function filterJustStrings(arr: (string| number)[]) { return arr.filter((i) => typeof i === 'string') as string[]; } Given predicate…
user1852503
  • 4,747
  • 2
  • 20
  • 28
1
vote
1 answer

Guard Clause Not Firing

So I have been trying to get guard clauses to work with Caliburn.Micro and a bound textbox. The View:
deanvmc
  • 5,957
  • 6
  • 38
  • 68
1
vote
3 answers

What is the syntax for different recursions depending on two boolean guards?

I'm very new to Haskell and am trying to write a simple function that will take an array of integers as input, then return either the product of all the elements or the average, depending on whether the array is of odd or even length,…
Watakashi
  • 13
  • 2
1
vote
2 answers

Parse error with my haskell function to identify if a number is prime

I am a beginner with Haskell and I can't seem to understand what is wrong with my function. I keep getting a parse error and I don't understand what is wrong with my code. isPrime :: Int -> Bool isPrime x | x <= 1 = False | x `mod` y == 0 for…
aralk
  • 73
  • 6
1
vote
1 answer

Testing truthiness in guards

I can use guards to test if an argument is true: defmodule Truth do def true?(term) when term, do: "#{term} is true" def true?(term), do: "#{term} is not true" end This works as expected for boolean values: Truth.true?(true) #=> "true is…
Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
1
vote
1 answer

"Non-exhaustive patterns in function" error when appending value before function call

I'm not sure what I'm not handling. Suppose I have a function, that converts an integer to a string. Call it converter. Now, to convert position integer to string, I just call converter. To convert a negative integer to string, I append - to the…
SilentDev
  • 20,997
  • 28
  • 111
  • 214