byers's profile安全第一-长风野老,铁臂山人PhotosBlogLists Tools Help
    August 07

    老狗学不会新把戏

    都说老狗学不会新把戏,为了不沦为老狗,我还是要保持不停地学习。
    最近在学习scala语言,一种面向对象的函数式语言,目前运行于jvm之上,可以与java互操作。
    官方提供了一个很不错的站点提供培训:http://www.simplyscala.com/
    为什么我决定学习一下这个语言呢?主要是以前很惭愧没有学习过函数式语言,正好就拿这个入门了。函数式的好处很多,例如下面的样例代码
     
    abstract class TreeN
    case class InterN(key:String,left:TreeN,right:TreeN) extends TreeN
    case class LeafN(key:String,value:Int) extends TreeN

    def find(t:TreeN,key:String):Int={
         t match {
             case InterN(k,l,r) => find((if(k>=key)l else r),key)
             case LeafN(k,v) => if(k==key) v else 0
        }
    }
    // create a binary tree
    val t=InterN("b",InterN("a",LeafN("a",1),LeafN("b",2)),LeafN("c",3))
    /*       [b]
             / \
           [a] c,3
           / \
         a,1 b,2
    */
     
    寥寥几行定义了一个二叉搜索树。核心的地方在于find函数这里,完全体现了函数式在这方面相较于命令式语言的优越性,就是在写递归的时候非常简洁。其他的我还是刚刚学习,还需要好好体会。

    Comments (2)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    瑜瑾 倪wrote:
    恩 不能做老狗…………我记得谁以前绰号叫老狗来着……
    Aug. 12
    Tian-Yi Shuiwrote:
    这种心态很值得提倡!!! 长期学习,真的很必要,但说起来容易做起来难
    Aug. 7

    Trackbacks

    The trackback URL for this entry is:
    http://lonegunman.spaces.live.com/blog/cns!9C763BD9399B0328!2104.trak
    Weblogs that reference this entry
    • None