Is it possible to use the if function template in Haskell?

I know that a method that accepts list input can use pattern matching like this:

testingMethod [] = "Blank"
testingMethod (x:xs) = show x

My question is: can I do this with the if function? For example, if I have a function1 that should check the output template from function2, how would I do it? I am looking for something like this:

if (function2 inputInt == pattern((x:xs), [1,2,3])) then

Can this be done in haskell?

+4
source share
2 answers

The syntax you are looking for is a case or guard argument:

You said:

if (function2 inputInt == pattern((x:xs), [1,2,3])) then

In Haskell:

testingMethod inputInt | (x:xs, [1,2,3]) <- function2 inputInt = expr
                       | otherwise = expr2

Or using caseand let(or you can embed or use where):

testingMethod inputInt =
    let res = function2 inputInt
    in case res of
         (x:xs, [1,2,3]) -> expr
         _ -> expr2

Or using view templates:

{-# LANGUAGE ViewPatterns #-}
testingMethod (function2 -> (x:xs,[1,2,3])) = expr
testingMethod _ = expr2
+6
source

- "" , case. , desugar :

testingMethod [] = "Blank"
testingMethod (x:xs) = show x

:

testingMethod arg = case arg of
  [] -> "Blank"
  (x:xs) -> show x

, , , case. :

case function2 inputInt of
  ((x:xs), [1,2,3]) -> -- ...
  (_, _) -> -- ...

, function2 .

+1

Source: https://habr.com/ru/post/1689413/


All Articles