在haskell中获取函数作为参数 [英] Get function as parameter in haskell

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

问题描述

我无法想象这一点,我有一个名为Enumeration的类型

 >类型Enumeration a = Int  - > [a] 

我需要对它进行映射。我写了下面这个函数:

 > imapE ::(a  - > b) - >枚举a  - >枚举b 
> imapE f(m fa)= \ n - > imapF f fa

其中 imapF 被定义为这:

 > imapF ::(a  - > b) - > [a]  - > [b] 
> imapF _ [] = []
> imapF f(x:xs)= fx:imapF f xs

但是当我尝试加载我的代码我在 imapE 函数中得到以下错误 BinaryTrees.lhs:91:14:模式中的分析错误:m

我试图将第一个枚举 Enumeration a 作为它的函数(Int和[a])

解决方案

您无法对函数进行模式匹配,但您不必这样做:

 > imapE ::(a  - > b) - >枚举a  - >枚举b 
> imapE f g =(imapF f)。 g

(好吧, imapF code> map 真的)。



不使用: p>

 > imapE ::(a  - > b) - >枚举a  - >枚举b 
> imapE f g = \ n - > imapF f(g n)


I can't figure this, I have a type called Enumeration

> type Enumeration a = Int -> [a]

And I need to map over it. I wrote this following function:

> imapE :: (a -> b) -> Enumeration a -> Enumeration b
> imapE f (m fa) = \n -> imapF f fa

where imapF is defined like this:

> imapF :: (a -> b) -> [a] -> [b] 
> imapF _ [] = []
> imapF f (x:xs) = f x : imapF f xs

but when I try to load my code I get the following error BinaryTrees.lhs:91:14: Parse error in pattern: m regarding my imapE function.

I am trying to get the first enumeration Enumeration a as the function it is (Int and [a])

解决方案

You cannot pattern match over a function, but you don't have to do that:

> imapE :: (a -> b) -> Enumeration a -> Enumeration b
> imapE f g = (imapF f) . g

(Well, imapF is just map really).

Without using .:

> imapE :: (a -> b) -> Enumeration a -> Enumeration b
> imapE f g = \n -> imapF f (g n)

这篇关于在haskell中获取函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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