1

I have this code that will return the index of a char in a char array but I want my function to return something like -1 if the value isn't in the array. As it stands the function returns the size of the array if the element isn't in the array. Any ideas on how to change my code in order to apply this feature?

I am trying not to use any fancy functions to do this. I just want simple code without built-in functions.

isPartOf :: [(Char)] -> (Char) -> Int
isPartOf [] a = 0
isPartOf (a:b) c
    | a == c = 0
    | otherwise = 1 + isPartOf b c

For example:

*Main> isPartOf [('a'),('b'),('c')] ('z') 
3

But I want:

*Main> isPartOf [('a'),('b'),('c')] ('z') 
-1
melpomene
  • 84,125
  • 8
  • 85
  • 148

4 Answers4

4

Let's try to define such a function, but instead of returning -1 in case of element being not a part of the list, we can return Nothing:

isPartOf :: Eq a => [a] -> a -> Maybe Int
isPartOf [] _ = Nothing
isPartOf (x : xs) a | x == a = Just 0
                     | otherwise = fmap ((+) 1) (isPartOf xs a)

So, it works like that:

>> isPartOf [('a'),('b'),('c')] ('z')
Nothing
it :: Maybe Int

>> isPartOf [('a'),('b'),('c')] ('c')
Just 2
it :: Maybe Int

After that we can use built-in function fromMaybe to convert the Nothing case to -1:

>> fromMaybe (-1) $ isPartOf [('a'),('b'),('c')] ('c')
2
it :: Int

>> fromMaybe (-1) $ isPartOf [('a'),('b'),('c')] ('z')
-1
it :: Int

In case you're curios if such a function already exist, you can use Hoogle for that, searching the [a] -> a -> Maybe Int function: https://www.haskell.org/hoogle/?hoogle=%5Ba%5D+-%3E+a+-%3E+Maybe+Int

And the first answer will be elemIndex:

>> elemIndex 'c' [('a'),('b'),('c')]
Just 2
it :: Maybe Int

>> elemIndex 'z' [('a'),('b'),('c')]
Nothing
it :: Maybe Int

Hope this helps.

3

The smallest change to achieve this is

isPartOf :: [Char] -> Char -> Int
isPartOf [] a = (-1)    -- was: 0
isPartOf (a:b) c
    | a == c = 0
    | otherwise = 1 +   -- was: isPartOf b c
          if  (isPartOf b c) < 0  then  (-2)  else  (isPartOf b c)

This is terrible computationally though. It recalculates the same value twice; what's worse is that the calculation is done with the recursive call and so the recursive call will be done twice and the time complexity overall will change from linear to exponential!

Let's not do that. But also, what's so special about Char? There's lots of stuff special about the Char but none are used here, except the comparison, (==).

The types the values of which can be compared by equality are known as those belonging to the Eq (for "equality") type class: Eq a => a. a is a type variable capable of assuming any type whatsoever; but here it is constrained to be such that ... yes, belongs to the Eq type class.

And so we write

isPartOf :: Eq a => [a] -> a -> Int
isPartOf [] a = (-1)
isPartOf (a:b) c
    | a == c    = 0
    | otherwise = let d = isPartOf b c in
                  1 + if  d < 0  then  (-2)  else  d

That (-2) looks terribly ad-hoc! A more compact and idiomatic version using guards will also allow us to address this:

isPartOf :: Eq a => [a] -> a -> Int
isPartOf [] a = (-1)
isPartOf (a:b) c
    | a == c    = 0
    | d < 0     = d 
    | otherwise = 1 + d
          where 
          d = isPartOf b c

Yes, we can define d in the where clause, and use it in our guards, as well as in the body of each clause. Thanks to laziness it won't even be calculated once if its value wasn't needed, like in the first clause.

Now this code is passable.

The conditional passing and transformation is captured by the Maybe data type's Functor interface / instance:

fmap f Nothing  = Nothing     -- is not changed
fmap f (Just x) = Just (f x)  -- is changed

which is what the other answer here is using. But it could be seen as "fancy" when we only start learning Haskell.

When you've written more functions like that, and become "fed up" with repeating the same pattern manually over and over, you'll come to appreciate it and will want to use it. But only then.

Yet another concern is that our code calculates its result on the way back from the recursion's base case.

But it could instead calculate it on the way forward, towards it, so it can return it immediately when the matching character is found. And if the end of list is found, discard the result calculated so far, and return (-1) instead. This is the approach taken by the second answer.

Though creating an additional function litters the global name space. It is usual to do this by defining it internally, in the so called "worker/wrapper" transformation:

isPartOf :: Eq a => [a] -> a -> Int
isPartOf xs c = go xs 0
   where
   go [] i = (-1)
   go (a:b) i
    | a == c    = i
    | otherwise = -- go b (1 + i)
                  go b $! (1 + i)

Additional boon is that we don't need to pass around the unchanged value c -- it is available in the outer scope, from the point of view of the internal "worker" function go, "wrapped" by and accessible only to our function, isPartOf.

$! is a special call operator which ensures that its argument value is calculated right away, and not delayed. This eliminates an unwanted (in this case) laziness and improves the code efficiency even more.

But from the point of view of overall cleanliness of the design it is better to return the index i wrapped in a Maybe (i.e. Just i or Nothing) instead of using a "special" value which is not so special after all -- it is still an Int.

It is good to have types reflect our intentions, and Maybe Int expresses it clearly and cleanly, so we don't have to remember which of the values are special and which regular, so that that knowledge is not external to our program text, but inherent to it.

It is a small and easy change, combining the best parts from the two previous variants:

isPartOf :: Eq a => [a] -> a -> Maybe Int
isPartOf .....
  .......
  .......  Nothing .....
  .......
  .......  Just i  .....
  .......

(none of the code was tested. if there are errors, you're invited to find them and correct them, and validate it by testing).

Will Ness
  • 70,110
  • 9
  • 98
  • 181
2

You can achieve it easily if you just pass current element idx to the next recursion:

isPartOf :: [Char] -> Char -> Int
isPartOf lst c = isPartOf' lst c 0

isPartOf' :: [Char] -> Char -> Int -> Int
isPartOf' [] a _ = -1
isPartOf' (a:b) c idx
    | a == c = idx
    | otherwise = isPartOf' b c (idx + 1)
Karol Samborski
  • 2,757
  • 1
  • 11
  • 18
1

You are using your function as an accumulator. This is cool except the additions with negative one. An accumulator cannot switch from accumulating to providing a negative 1. You want two different things from your function accumulator. You can use a counter for one thing then if the count becomes unnecessary because no match is found and a negative 1 is issued and nothing is lost. The count would be yet another parameter. ugh. You can use Maybe but that complicates. Two functions, like above is simpler. Here are two functions. The first is yours but the accumulator is not additive it's concatenative.

cIn (x:xs) c | x == c    =  [1]
             | null xs   = [-1]
             | otherwise = 1:cIn xs c


Cin ['a','b','c'] 'c'

[1,1,1]

cIn ['a','b','c'] 'x'

[1,1,-1]

So the second function is

f ls = if last ls == 1 then sum ls else -1 

It will

f $ Cin ['a','b','c'] 'c'

3

and

f $ Cin ['a','b','c'] 'x'

-1

You can zero the index base by changing [1] to [0]

fp_mora
  • 718
  • 6
  • 11