检查Haskell中的函数的内部 [英] Examining the internals of the functions in Haskell

查看:120
本文介绍了检查Haskell中的函数的内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的新手,虽然有过Lisp / Scheme的经验。现在我正在看SICP的例子,并试图在Haskell中实现它们以获得更多的实践经验。在讲座3b中,作者提出了一个函数来象征性地计算衍生物。它包含以下几行:

 (define(deriv exp var)
(cond((constant ($ var $)
((same-var?exp var)1)
; ...

进一步在讲座中,定义了一些更多的函数:

$ $ $ $ $ $ $ $ $(define(constant?exp var )
(和(atom?exp)
(not(eq?exp var))))

有没有办法在Haskell中做同样的事情,也就是检查一些其他函数的原子性和符号等价性吗?或者更一般的说,Haskell中的反汇编函数的方法是什么?

解决方案

您的Scheme示例并不真正检查Scheme函数。最近,我在Haskell中对以下类型的值进行了一些符号区分:

  data Exp a = Lit a 
| Exp a:*:Exp a
| Exp a:+:Exp a
| Var字符串
导出Eq

不用使用 atom? eq?您使用 case (或其他模式匹配)和 ==


I am a Haskell newbie, though had a previous Lisp/Scheme experience. Right now I am looking at the examples from SICP and trying to implement them in Haskell to get more hands-on experience. In the lecture 3b authors present a function for computing the derivatives symbolically. It contains, among others, the following lines:

(define (deriv exp var)
    (cond ((constant? exp var) 0)
          ((same-var? exp var) 1)
; ...

Further in the lecture, some more functions are defined:

(define (constant? exp var)
    (and (atom? exp)
         (not (eq? exp var))))

Is there a way to do same thing in Haskell, i.e. check for atomicity and symbolic equivalence to some other function? Or more general, what are the means of "disassembling" functions in Haskell?

解决方案

Your Scheme examples don't actually examine Scheme functions. I recently did some symbolic differentiation in Haskell over values of the following type:

data Exp a = Lit a
           | Exp a :*: Exp a
           | Exp a :+: Exp a
           | Var String
  deriving Eq

Instead of discriminating using atom? or eq? you use case (or other pattern matching) and ==.

这篇关于检查Haskell中的函数的内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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