我很难在Elm中写一个简单的if-then语句,涉及信号。
如果条件本身是Signal
类型怎么办?我想更改Elm网站上的Mouse Down示例:
import Graphics.Element exposing (..)
import Mouse
main : Signal Element
main =
Signal.map show Mouse.isDown
根据鼠标是向上还是向下,它会说 True 或 False 。如果我想要它说“Up”或“Down”怎么办?我的布尔函数可以说:
<!-- language: haskell -->
f : Bool -> String
f x =
if x then "↑" else "↓"
但是当我更改主要功能时,我得到了类型不匹配。
<!-- language: haskell -->
main : Signal Element
main =
Signal.map show ( f Mouse.isDown)
错误#1:
The 2nd argument to function `map` has an unexpected type.
10| Signal.map show ( f Mouse.isDown)
As I infer the type of values flowing through your program, I see a conflict
between these two types:
Signal a
String
错误#2:
The 1st argument to function `f` has an unexpected type.
10| Signal.map show ( f Mouse.isDown)
As I infer the type of values flowing through your program, I see a conflict
between these two types:
Bool
Signal Bool
答案 0 :(得分:3)
与show :: Bool -> Element
基本相同。您没有将Signal传递给该函数,而是将map
函数传递给Signal。它与f
:
import Mouse
import Graphics.Element exposing (Element, show)
f : Bool -> String
f x = if x then "↑" else "↓"
updown : Signal String
updown = Signal.map f Mouse.isDown
main : Signal Element
main = Signal.map show updown
或简而言之,作文:main = Signal.map (show << f) Mouse.isDown
。