ELM Models: How Can I Transfer A Value Into A List Of Values?

时间:2017-08-05 11:01:07

标签: elm

I want to add a certain value to a list. Both are inside my ELM-model:

type alias Model =
{   syllables : List Syllable
,   words : List Word
,   newSyllable : String
,   newWord : String
}

I want to add the newSyllable value to the list of syllables, when I click the button.

I placed this attribute inside my view:

onClick TransferSyllable

Everything works right, but I wonder how I can transfer one value of my model into the list of values!?

Thanks.

EDIT:

This is my definition of "Syllable":

type alias Syllable =
{   content : String
,   start : Bool
,   mid : Bool
,   end : Bool
}

I want to insert the value to the end of the list.

1 个答案:

答案 0 :(得分:1)

由于Syllable是包含四个字段的记录类型,而newSyllable只是一个字符串,因此您需要一个将String转换为Syllable的函数。我假设该函数具有签名:

makeSyllable : String -> Syllable

可以使用List.append将音节添加到列表末尾。由于append需要List a,因此在将newSyllable传递给append时,您需要在{ model | syllables = List.append model.syllables [ makeSyllable model.newSyllable ] } 周围添加括号:

{{1}}