reverse : List a -> List a reverse list = case list of [] -> [] (x::xs) -> reverse xs ++ [x]
quicksort : List comparable -> List comparable quicksort list = case list of [] -> [] (x::xs) -> let smallerSorted = quicksort (List.filter ((>) x) xs) biggerSorted = quicksort (List.filter ((<=) x) xs) in smallerSorted ++ [x] ++ biggerSorted
可以找到规律:
Usually you define an edge case and then you define a function that does something between some element and the function applied to the rest.
更具体的解释可以看参考链接3。
控制结构
Elm中没有for和while循环(都通过递归实现了)。但是存在if和case语句。
1 2 3 4 5 6 7 8 9 10 11 12 13
if powerLevel > 9000then"OVER 9000!!!"else"meh" case maybe of Just xs -> xs Nothing -> []
case xs of hd::tl -> Just (hd,tl) [] -> Nothing
case n of 0 -> 1 1 -> 1 _ -> fib (n-1) + fib (n-2) -- _表示不接受入参
view : Model -> HtmlMsg view model = div [] [ input [ placeholder "Text to reverse", onInput Change ] [] , div [] [ text (String.reverse model.content) ] ]
decodeString : Decoder a -> String -> ResultString a
由第一个参数指定decoder类型,如 decodeString int "42"就指定了一个整数的解析器。在多数情况下,JSON字符串并没这么简单。这时,需要先利用基本的int、string、list、dict解析器结合map2、map3等构造相应的Decoder,再交给decodeString处理。函数返回Result类型。
1 2 3 4 5 6 7
import Json.Decode exposing (..)
typealiasPoint = { x : Int, y : Int }
pointDecoder = map2 Point (field "x" int) (field "y" int)
decodeString pointDecoder """{ "x": 3, "y": 4 }"""-- Ok { x = 3, y = 4 } : Result String Point
CSS Reset 是革命党,CSS Reset 里最激进那一派提倡不管你小子有用没用,通通给我脱了那身衣服,凭什么你 body 出生就穿一圈 margin,凭什么你姓 h 的比别人吃得胖,凭什么你 ul 戴一胳膊珠子。于是 *{margin:0;} 等等运动,把人家全拍扁了。看似是众生平等了,实则是浪费了资源又占不到便宜,有求于人家的时候还得贱贱地给加回去,实在需要人家的默认样式了怎么办?人家锅都扔炉子里烧了,自己看着办吧。
XSS(Cross Site Script,跨站脚本攻击),分为反射式,存储式,前者只对特定用户生效,如存储在个人资料中的脚本,后者对访问网站的所有用户生效,如攻击站点本身代码。防御转义时,不仅要监测<script>标签,对可以书写JavaScript的href属性,src属性,甚至img的onerror等事件也要做防御。
将四个数字按a-b-c-d这样连接起来(位于大数进制的数字系统中),构成特异性。所使用的进制取决于上述类别中的最高计数。最终决定优先级顺序。简而言之就是,style > id > class > tag > pseudo class,统计情况下看个数,有!important时,以!important为准。
34. 连等的赋值顺序
问题是,有这样一段代码,问原理是什么:
1 2 3 4
var foo = { n: 1 }; var bar = foo; foo.x = foo = { n: 2 }; console.log(foo.x); // undefined
<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01 Frameset//EN""http://www.w3.org/TR/html4/frameset.dtd">
在HTML5中,只有一种doctype可以选择,那就是html:
1
<!DOCTYPE html>
44. standard mode和quirks mode
分别是标准模式和怪异模式。由于历史原因,为了兼容标准建立前就已存在的古老网站,浏览器存在着两种解析网页的模式。在怪异模式下,排版会模拟Navigator 4与Internet Explorer 5的非标准行为。为了支持在网络标准被广泛采用前,就已经建好的网站,这么做是必要的。在标准模式下,行为即(但愿如此)由HTML与CSS的规范描述的行为。在<!DOCTYPE>中指定html将自动启用标准模式。这里有更多介绍。