试着学习玻璃钢和榆树。从玻璃钢和榆树的角度看下面的节目"ok“吗?还有什么可以改进的?这个程序可以在这里执行:http://elm-lang.org/try
import Random
import Mouse
data Fruit = Apple | Orange | Banana | Melon | Guava
rand = Random.range 0 4 (Mouse.clicks)
fruit n = if | n == 0 -> Apple
| n == 1 -> Orange
| n == 2 -> Banana
| n == 3 -> Melon
| otherwise -> Guava
fruitText = toForm <~ (asText <~ (fruit <~ rand))
time = lift (inSeconds . fst) (timestamp (fps 40))
scene dy form = collage 300 300 [move (0, 100 * cos dy) form]
main = lift2 scene time fruitText发布于 2013-10-10 09:27:38
迟了两个月,但我还是会尽力回答的。;-)
由于您的rand已经确定了数字将是有效的索引,您的水果,您不必使用这么多,如果情况。其余的在我看来还不错,但我在源代码中做了一些注释。
import Random
import Mouse
-- Sometimes empty lines can improve readability.
data Fruit = Apple | Orange | Banana | Melon | Guava
-- For module global declarations, it is often helpful to
-- explicitly annotate the types.
fruits : [Fruit]
fruits = [Apple, Orange, Banana, Melon, Guava]
-- especially for functions :)
fruit : Int -> Fruit
fruit n = head <| drop n fruits
rand : Signal Int
rand = Random.range 0 ((length fruits) - 1) Mouse.clicks
-- exercise: Try to figure and annotate the remaining types. ;-)
fruitText = toForm <~ (asText <~ (fruit <~ rand))
time = lift (inSeconds . fst) (timestamp (fps 40))
scene dy form = collage 300 300 [move (0, 100 * cos dy) form]
main = lift2 scene time fruitTextbtw:较让某人复制粘贴到elm-lang.org/try,向某人显示elm代码要容易一些。
请参阅:http://share-elm.com/sprout/52567327e4b0d6a98b1531c7
https://codereview.stackexchange.com/questions/29823
复制相似问题