Code Whisperer
Code Whisperer

Reputation: 23652

How to Build a List Using a Loop in Elm?

Let's say I have two lists, list letters, containing the letters A-F, and list nums, containing the numbers 1-6.

In Elm, how can I programatically make a list containing every possible combination (i.e, A1, C6, F3, D2, etc.)?

This is just for the purpose of code elegance, hardcoding every possible combination would be equivalent.

In JavaScript, it would be represented by something like...

const nums = [1,2,3,4,5,6];
const letters = [`a`,`b`,`c`,`d`,`e`,`f`];

const combineLists = (a,b)=>{
  const newList = [];
  a.forEach(aEl=>{
    b.forEach(bEl=>{
      newList.push(aEl + bEl);
    })
  })
  return newList;
}
          
console.log(combineLists(letters,nums));

How would you write an equivalent combineLists function in Elm?

Upvotes: 3

Views: 5112

Answers (3)

gabrielperales
gabrielperales

Reputation: 7755

I'm not sure that this is the best implementation, but you can try this:

import Html exposing (..)


nums : List Int
nums = 
    [ 1, 2, 3, 4, 5, 6 ]


letters: List String
letters = 
    [ "a", "b", "c", "d", "e", "f" ]


combineLists : List a -> List b -> List String
combineLists list1 list2 =
    List.concatMap
        (\elList1 -> 
            List.map 
                (\elList2 -> toString elList1 ++ toString elList2) 
                list2 
        )
        list1


main = 
    text <| toString <| combineLists letters nums

This could be another way to do the same:

import Html exposing (..)


nums : List Int
nums = 
    [ 1, 2, 3, 4, 5, 6 ]


letters: List String
letters = 
    [ "a", "b" , "c" , "d", "e", "f" ]


combineLists : List a -> List b -> List String
combineLists list1 list2 =
    List.map toString list1
        |> List.concatMap
            (\e1 ->
                List.map (toString >> String.append e1) list2
            )


main = 
    text <| toString <| combineLists letters nums

Upvotes: 3

Simon H
Simon H

Reputation: 21005

Here is my suggestion, which I think is the most condensed possible

module Main exposing (..)

import Html exposing (..)


nums : List Int
nums =
    [ 1, 2, 3, 4, 5, 6 ]


letters : List String
letters =
    [ "a", "b", "c", "d", "e", "f" ]


main =
    nums
        |> List.map toString
        |> List.concatMap (\n -> List.map (String.append n) letters)
        -- or in point free style
        -- |> List.concatMap (String.append >> flip List.map letters)
        |> toString
        |> text

Point free style does not seem to have the same pride of place in Elm as it does in Haskell, but I include it for completeness and is the way I would write my code

Upvotes: 9

Dave Compton
Dave Compton

Reputation: 1441

Here's another possibility using andThen from List.Extra

import Html exposing (text)
import List.Extra exposing (andThen) 

nums : List Int
nums =
    [ 1, 2, 3, 4, 5, 6 ]


letters : List String
letters =
    [ "a", "b", "c", "d", "e", "f" ]

main =
    andThen 
      (\num -> andThen 
                 (\letter -> [(toString num) ++ letter]) 
                 letters ) 
      nums
    |> toString
    |> text

Upvotes: 3

Related Questions