Expression syntax - Haskell

I'm new to Haskell !! I wrote this code:

import Data.List
inputIndex :: [String] -> [String] -> Bool
inputIndex listx input = and [x `elem` listx |x <- input]
inputIndex = if inputIndex == true
                then putStrLn ("ok")

      

It works fine without the instruction if

, but when I put the statement if

, the following error is displayed:

Syntax error in expression (unexpected `} ', possibly due to poor linking)

What am I doing wrong here?

thanks

0


a source to share


2 answers


Here are a couple of errors:

  • You will need an else clause.
  • True

    must be capitalized.
  • inputIndex

    must always take two arguments (currently it doesn't, in the latter case).

I think you want something like this ...

inputIndex :: [String] -> [String] -> IO ()
inputIndex listx input = if inputIndex' listx input
                             then putStrLn ("ok")
                             else putStrLn ("not ok")
  where
    inputIndex' :: [String] -> [String] -> Bool
    inputIndex' listx input = and [x `elem` listx |x <- input]

      



(Here I have defined a new function with a nearly identical name, adding prime / apostrophe. By defining it in the sentence where

, it is only visible to the outer inputIndex

function. Helper function if you do. I could have chosen a completely different name as well, but I'm not creative.)

You can also condense this into the following (which is also more general):

allPresent :: (Eq t) => [t] -> [t] -> IO ()
allPresent xs ys = putStrLn (if and [y `elem` xs | y <- ys] then "ok" else "not ok")

      

+8


a source


  • This is "truth", not "truth".
  • The second implementation of inputIndex is incompatible with the first. All your templates for a function must have the same signature ([String] → [String] → Bool)
  • The error displayed here is not generated by this code because there is no '}' here.
  • putStrLn is signed String -> IO()

    , while yours inputIndex

    looks like it should be clean - just return the value and print it somewhere else.


0


a source







All Articles