经验丰富的Schemer,letcc和guile [英] The Seasoned Schemer, letcc and guile

查看:109
本文介绍了经验丰富的Schemer,letcc和guile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有几个问题,关于 letcc 用在经验丰富的Schemer中。

 < code $($($)$(b $($($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (car set))(hop'())
((null?(cdr sets))(car sets))
(else
(intersect(car sets)
(A(cdr sets)))))))
;为简洁而删除相交点的定义
(cond
((null?sets)'())
(A集))))))




  1. 我知道 letcc 实现了什么,并且基本上类似于 catch throw 在ruby中(貌似CL),这基本上意味着可以通过调用任何名为 letcc 的方法来缩减整个代码块。我在这个笑中碰到过的功能性事物rt系列书籍,这让我感觉有点犹豫,因为我想学习一个很好的功能风格。我只是误解 letcc ,还是它不是一个真正的函数式编程概念,只是为了提高性能而存在?整个想法,我可以在一些例程的中间,然后突然到代码中的另一个点感觉有点不对......像滥用Java中的try / catch程序流程。

  2. b $ li

    letcc 似乎不存在于我在OS X中安装的guile(1.8.7)版本中。是否有另一个名称如果我误解 letcc ,那么我应该在guile中寻找它? 它用Java中的try / catch或者ruby中的catch / throw(这是 not 异常处理,对于非rubyist来说很清楚),它在功能级上的工作原理?它能以更长,更复杂的方式表达出来,让我相信它是有用的吗? 方法


  1. 功能有几个含义,但没有任何流行的含义与任何方式的延续相矛盾。但是他们可能会被滥用到创建难以阅读的代码中。它们不是可被滥用于程序流的工具 - 它们是程序流工具。


  2. 不能帮助你。我知道在Guile的延续中有半个月的时间,但我不知道事情的立场。它肯定应该有 call-with-current-continuation ,通常也在 call / cc 更友好的名字下,和 let / cc 是一个简单的宏,可以使用 call / cc 构建。



    我可以告诉你,在Racket中有一个 let / cc 内置其他内置 .racket-lang.org / reference / cont.htmlrel =noreferrer>同族,另外还有一个整个库的各种控制操作符(带有广泛的引用列表)。


  3. let / cc 的简单用法确实类似于catch / t打出一些东西 - 更具体地说,这样的延续通常被称为延续延续(或有时是向上)。这是你在代码中使用的一种方式,通常用于实现 abort 或者 return



    但Scheme中的延续是可以在任何地方使用的东西。对于一个显示这种差异的简单例子,试试这个:

     (define(foo f)(f 100))
    (let / cc k(+(foo k)junk)(more junk))


  4. 最后,如果您想了解更多关于延续的信息,可以查看 PLAI 的相关部分,还有更多的简短的by- Matthew Might编写的示例概述,您可以看到我编写的一些课堂笔记这些都是基于PLAI的,其中一些例子受到了后一篇文章的启发。



A few questions here, regarding letcc that is used in The Seasoned Schemer.

(define (intersect-all sets)
  (letcc hop
    (letrec
      ((A (lambda (sets)
            (cond
              ((null? (car sets)) (hop '())
              ((null? (cdr sets)) (car sets))
              (else
                (intersect (car sets)
                           (A (cdr sets)))))))
       ; definition of intersect removed for brevity
      (cond
        ((null? sets) '())
        (else (A sets))))))

  1. I think I understand what letcc achieves, and that is basically something like catch and throw in ruby (and seemingly CL), which basically means a whole block of code can be cut short by calling whatever the named letcc is. This feels like the least "functional" thing I've come across in this short series of books and it makes me feel a bit hesitant to use it, as I want to learn a good functional style. Am I just misunderstanding letcc, or is it not really a functional programming concept and only exists to improve performance? The whole idea that I can be in the middle of some routine and then suddenly get to another point in the code feels a bit wrong... like abusing try/catch in Java for program flow.

  2. letcc doesn't seem to exist in the version of guile (1.8.7) I have installed in OS X. Is there another name for it that I should be looking for in guile?

  3. If I'm misunderstanding letcc by comparing it with try/catch in Java, or catch/throw in ruby (which is not exception handling, just to be clear, for the non-rubyists), how exactly does it work, at the functional level? Can it be expressed in a longer, more complex way, that convinces me it is functional after all?

解决方案

  1. "Functional" has several meanings, but no popular meaning contradicts continuations in any way. But they can be abused into creating code that is hard to read. They're not tools that can be "abused for program flow" -- they are program flow tools.

  2. Can't help you there. I know that there was semi-recent on continuations in Guile, but I don't know where things stand. It should definitely have call-with-current-continuation, usually also under the more friendly name of call/cc, and let/cc is a simple macro that can be built with call/cc.

    I can tell you that in Racket there is a let/cc builtin together with a bunch of others builtins in the same family, and additionally there's a whole library of various control operators (with an extensive list of references.).

  3. Simple uses of let/cc are indeed similar to a catch/throw kind of thing -- more specifically, such continuations are commonly known as "escape continuations" (or sometimes "upward"). This is the kind of use that you have in that code, and that is often used to implement an abort or a return.

    But continuations in Scheme are things that can be used in any place. For a very simple example that shows this difference, try this:

    (define (foo f) (f 100))
    (let/cc k (+ (foo k) "junk") (more junk))
    

  4. Finally, if you want to read more about continuations, you can see the relevant parts of PLAI, there's also a more brief by-example overview that Matthew Might wrote, and you can see some class notes that I wrote that are based on PLAI, with some examples that were inspired by the latter post.

这篇关于经验丰富的Schemer,letcc和guile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆