re: Daily Challenge #76 - Bingo! (or not...) VIEW POST

FULL DISCUSSION
 

Elm

module Bingo exposing (bingo)


belongsTo : List Int -> Int -> Bool
belongsTo integerList integer =
    List.member integer integerList


bingo : List Int -> Bool
bingo input =
    List.all (belongsTo input) [ 2, 9, 14, 7, 15 ]

Eplainations

module Bingo exposing (bingo)

Here we say that we only want the function bingo to be exposed to the outside world.

belongsTo : List Int -> Int -> Bool
belongsTo integerList integer =
    List.member integer integerList

We then define a helper function belongsTo which will not be exposed to the outside world.

It takes two parameters: the first being a list (array) of integers, and the second is an integer. The function will then return a boolean indicating if the integer is indeed part of the integerList thanks to the List.member function.

bingo : List Int -> Bool
bingo integerList =
    List.all (belongsTo integerList) [ 2, 9, 14, 7, 15 ]

Finally, we define our main function bingo which takes a list of integers. The List.all takes a function applied to each one of our BINGO list (which is just the integer representation of that word). If there is no BINGO integer in the integerList, it will return false. List.all expects all applied functions to return True. If only one of them return False (meaning, one of the BINGO integer has not been found in the integerList), it will return false.

Tests

module BingoTest exposing (suite)

import Bingo exposing (bingo)
import Expect exposing (equal)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "Basic tests"
        [ test "It should return true when passing a valid array" <|
            \_ ->
                equal True <| bingo [ 21, 13, 2, 7, 5, 14, 7, 15, 9, 10 ]
        , test "It should return false when passing an invalid array" <|
            \_ ->
                equal False <| bingo [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
        ]

Try it online.

code of conduct - report abuse