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.
答案 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}}