1

I have the following code:

findPerson name peeps = List.foldl
    (\a b -> case b of
        Just _ -> b
        Nothing -> if a.name == name then
            Just a
            else Nothing
    ) Nothing peeps

I would like to log the values of a and b inside the foldl. I've tried:

findPerson : String -> List Person -> Maybe Person
findPerson name peeps = List.foldl
    (\a b ->
        Debug.log(a)
        Debug.log(b)
        case b of
            Just _ -> b
            Nothing -> if a.name == name then
                Just a
                else Nothing
    ) Nothing peeps

However, this throws an error

I am looking for one of the following things:

a closing paren ')'
whitespace`

What am I doing wrong, and how can I log the values inside foldl?

Rich
  • 5,603
  • 9
  • 39
  • 61

2 Answers2

3

You can use a let in block for debugging.

let
    _ = Debug.log "a" a
    _ = Debug.log "b" b
in
    case b of
...

A function (or lambda) can only return once. Debug.log returns the second argument unchanged, so you have to pattern match it against something - and because you don't need the argument twice, but the side effect of Debug.log, you can pattern match it against _ (ignore).

farmio
  • 8,883
  • 3
  • 13
  • 17
2

You can also put the Debug.log directly inside the case statement, or inside the if statement for the same reasons @farmio mentioned :) - Like so :

findPerson name peeps = List.foldl
    (\a b -> 
        case ( Debug.log "inspect b: " b ) of
           Just _ -> 
              b
           Nothing -> 
              if ( Debug.log "person name is: " a.name )  == name then
                  Just a
              else 
                  Nothing
    ) Nothing peeps

Not as clean, but sometimes more useful because is more compact.

AIon
  • 12,521
  • 10
  • 47
  • 73