功能性程序通过类型分析“自己写”的例子 [英] Examples of functional programs 'writing themselves' via type analysis

查看:177
本文介绍了功能性程序通过类型分析“自己写”的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(背景:我一直在考虑做关于F#和函数式编程的演示,根据经验,我认为模式匹配和类型推断的'哇'因素不足以抵消'帮助'我的代码会从边缘掉下来!这让我想到了真正的哇因素 - 对我来说 - 这是1)如果它编译,通常意味着它的工作原理和2)你可以经常从这些类型推断出实现)



有一个 video 在Channel9上与Brian Beckman和Erik Meijer在一起,他们提到实现有时只是脱离了函数的类型签名。过去我也经历过这种情况,但不能提出一个很好的例子,它可以非常简单地呈现给没有以前的功能体验的人。



有没有人有一个很好的例子来分享? (它不一定要在F#中)
$ b $ p UPDATE



如果这有什么帮助,我想我们需要考虑这个问题:真正的困惑如下:

我有一些给定类型的数据,我想转换它转换为其他类型,我有一组函数,并带有给定的符号。



这是您必须连接在一起的'lego'。

解决方案

从最简单的功能开始: identity ::'a - > 一。你能想到多少个实现?如果你给我一个 a ,我只能用它做一件事来给你一个 a 。我给你回到你给我的同样的 a ,所以:

  let id x = x 

成对出现同样的情况。 fst ::('a,'b) - > 一。你可以采用多少种方式来实现?如何 snd ::('a,'b) - > B ?每一个只能有一个实现。



类似地,将列表的头部和尾部从 fst snd 。如果 head ::'列表 - > a tail ::'列表 - > '一个列表和一个'列表只是一对('a,'列表)(或空列表),那么显然为了满足这些类型,分别返回列表的第一部分和第二部分。

再举一个例子使用高阶函数: compose ::('a - >'b) - > ('c - >'a) - > 'c - > B 。只有一个实现,它完全脱离这些类型。给你一个 c 和两个函数。你可以用 c 来做什么?那么,你可以申请(c - > a)。那么你可以用 a 来做什么?你可以做的唯一事情就是申请(a - > b),并且瞧,你已经满足了这个类型。

  let compose fgx = f(gx)


(Background: I've been thinking about doing a presentation on F# and functional programming. From experience, I think that the 'wow' factor of pattern matching and type inference is not necessarily enough to counteract the 'help!' factor of "where are my curly brackets and semicolons, my code is going to fall off the edge!". Which got me thinking about the real wow factor - for me - which is 1) that if it compiles, generally that means that it works and 2) that you can often infer the implementation from the types)

There is a video on Channel9 with Brian Beckman and Erik Meijer where they mentioned how implementation sometimes just 'falls out' of the type signature of a function. I've also experienced this in the past, but can't come up with a good example that would be sufficiently simple to present to someone with no previous functional experience.

Has anyone got a good example to share? (it doesn't have to be in F#)

UPDATE

If it's any help, I think we need to think about this differently: The actual puzzle is as follows:

I have some data with a given type, I want to transform it to a different type, and I have a set of functions with given signaures.

This is the 'lego' that you have to plug together.

解决方案

Start with the simplest possible function: identity :: 'a -> 'a. How many implementations can you think of? If you give me an a, there's only one thing I can do with it to give you back an a. I give you back the same a that you gave me, so:

let id x = x

Same goes for pairs. fst :: ('a,'b) -> 'a. How many ways can you implement that? How about snd :: ('a, 'b) -> 'b? Only one implementation can possibly exist for each.

Analogously, taking the head and tail of a list falls right out of fst and snd. If head :: 'a list -> a and tail :: 'a list -> 'a list, and an 'a list is just a pair ('a, 'a list) (or the empty list), then it's obvious that to satisfy the types, you return first and second part of the list, respectively.

One more example to do with higher-order functions: compose :: ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b. There's only one implementation, and it falls right out of the types. You're given a c and two functions. What can you do with the c? Well, you can apply (c -> a). What can you then do with the a? The only thing you can do is apply (a -> b), and voila, you have satisfied the type.

let compose f g x = f (g x)

这篇关于功能性程序通过类型分析“自己写”的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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