• erlang学习1


     

    q().----退出erlang's console

    f().----撤销遍历绑定

    pwd().--打印当前目录

    cd("f:/sinpo").--进入指定目录

    c(geometry).--编译文件

    geometry:sum(L).--列表求和

    Z = fun(X) -> 2 * X end. --匿名函数

    %% erl中实现for循环
    for(Max,Max,F) -> [F(Max)];
    for(I,Max,F) ->[F(I)|for(I+1,Max,F)].

    %%map定义
    map(_,[]) -> [];
    map(F,[H|T]) -> [F(H)|map(F,T)].

    %%断言guard,是强化模式匹配的一种结构

    max(X,Y) when X > Y -> X;
    max(X,Y) -> Y.

    %%列表record
    record定义: (record定义在.erl源码或.hrl文件中)
         -record(todo, {status=reminder,who=joe,text}).
    rr("records.hrl"). ----console控制台导入.hrl record

    X1 = #todo{status=urgent,text = "this is a test!"}. ---record创建及更新赋值

    X2 = X1#todo{status = done}. ---创建X1副本,并改变status字段的值为原子:done

    #todo{who = W,text = Txt} = X1. ---模式匹配获得record中字段

    X1#todo.text ---点语法获得单个字段

    rf().---告诉shell,注释掉记录定义(记录就是元组,只不过使命名元组中不同元素的语法简化而已)

    %%case表达式(Guard1->为可选断言)

    case Expression of
         Pattern1 [when Guard1] -> Expr_seq1;
         Pattern1 [when Guard2] -> Expr_seq2;
         ...
    end

    例子:filter函数定义

    filter(P,[H|T]) ->
        case P(H) of
             true -> [H|filter(P,T)];
             false -> filter(P,T)
        end;
    filter(P,[]) ->
        [].

    %%if表达式(Guard1->断言必须)
    根据断言验证成功与否决定执行分支
    if
       Guard1 ->
           Expr_seq1;
       Guard2 ->
           Expr_seq2;
       ...
    end

    注:通常if表达式的最后一个断言为true,可保证表达式最后一个分支被求值。

  • 相关阅读:
    double类型的数值计算
    模拟ATM的功能
    自学Java第七周的总结
    自学java第六周的总结
    自学Java第五周的总结
    自学Java第四周的总结
    自学Java第三个星期的总结
    自学Java第二周的总结
    python-----静态方法
    python----类方法
  • 原文地址:https://www.cnblogs.com/sinpo/p/2243978.html
Copyright © 2020-2023  润新知