高阶函数用于输入函数列表和元素列表,并将函数应用于元素 [英] High order function thats has for input a list of functions and a list of elements and applies the functions to the elements

查看:92
本文介绍了高阶函数用于输入函数列表和元素列表,并将函数应用于元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我试图实现一个高位函数,声明为

  Ord u => [v-> u]  - > [v]  - > [u] 

输入a)任何类型的函数的列表和任何类型的值的范围,以及b)相同类型的元素的列表,然后它将返回一个列表,该列表是从应用函数发生的所有元素的结果给定列表给定列表中的元素从小到大排列顺序没有重复值。

我试图用foldr函数实现它,但没有运气。
我认为我可以使用zip将这些函数作为一对索引进行索引,以便它们将与foldr函数一一应用。波纹管,我创建了一个插入排序,所以我可以排序最终列表

  apply :: Ord u => [v-> u]  - > [v]  - > [u] 
apply fy = insSort(foldr(\(i,x)y - > x:y)(zip [ 。] f))

insSort :: Ord u => [u] - > [u]
insSort(h:t)=插入h(insSort t)
insSort [] = []

insert :: Ord u => u - > [u] - > [u]
插入n(h:t)
| n< = h = n:h:t
|否则= h:插入nt
插入n [] = [n]

输入与输出:

$ $ p $ $ $ c $>> apply [abs] [-1]
[1]

> apply [(^ 2)] [1..5]
[1,4,9,16,25]

> apply [(^ 0) ,(\ x-> div xx),(\ x-> mod xx)] [1..1000]
[0,1]

> apply [head.tail,last.init] [abc,aaaa,cbbc,cbbca]
abc

>申请[(^ 2),(^ 3),(^ 4),(2 ^)] [10]
[100,1000,1024,10000]

> (* 5)](适用[('div'5)] [1..100])
[0,5,10,15,20,25,30,35,40,45,50,55 ,60,65,70,75,80,85,90,95,100]


解决方案

  apply :: [a  - > b]  - > [a]  - >首先,这个签名与标准的  $> 

$> //hackage.haskell.org/package/base-4.9.1.0/docs/Control-Applicative.html#v:-60--42--62-rel =nofollow noreferrer> < ; *>
函数,它是 Applicative class。

  class Applicative f where 
pure :: a - > f a
(*):: f(a - > b) - > f a - > fb

设置 f〜[] 我们有< * GT; :: [a - > b] - > [a] - > [b] 。



至少有两种明智的方式来编写 Applicative 列表实例。第一个是输入笛卡尔乘积,将每个函数与每个值进行配对。如果<> 的输入列表具有长度 N M ,则输出列表将具有长度 N * M个 pure 对于这个规范来说将会把一个元素放在一个单例列表中,这样 pure id <*> xs = xs

 实例Applicative []其中
pure x = [x]
(f:fs)* xs = map f xs ++(fs * xs)

这相当于< href =https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#line-739 =nofollow noreferrer>标准 Applicative > []



另一种合理的方式实现 Applicative 通过逐点应用函数将两个列表压缩在一起。如果<> 的输入列表具有长度 N M ,则输出列表将具有长度 min(N,M)创建一个无限列表,所以再一次纯id <*> xs = xs

 实例Applicative []其中
pure x = let xs = x:xs in xs
[] * _ = []
_< *> [] = []
(f:fs)* (x:xs)= fx:(fs * xs)

此实例可用在 base ZipList newtype


As the title suggests i am trying to implement a high order function declared as

Ord u => [v->u]->[v]->[u]

that has inputs a) a list of functions of any type and a range of values of any type and b) a list of elements of the same type and then it will return a list that is the result of all elements that occured from applying a function from the given list to an element from the given list in ascending order without repetitive values.

i was trying to implement it with the foldr function with no luck. i thought that i can index with zip the functions as a pair so they will be applied one by one with the foldr function. bellow that i created a insertion sort so i can sort the final list

apply :: Ord u => [v->u]->[v]->[u]             
apply f y = insSort (foldr(\(i, x) y -> x:y ) (zip [1..] f))

insSort :: Ord u => [u] -> [u]
insSort (h:t) = insert h (insSort t)
insSort [] = []

insert :: Ord u => u -> [u] -> [u]
insert n (h:t)
            | n <= h = n : h : t
            | otherwise = h : insert n t
insert n [] = [n]

for example some inputs with the output:

>apply [abs] [-1]
[1]

>apply [(^2)] [1..5]
[1,4,9,16,25]

>apply [(^0),(0^),(\x->div x x),(\x->mod x x)] [1..1000]
[0,1]

>apply [head.tail,last.init] ["abc","aaaa","cbbc","cbbca"]
"abc"

> apply [(^2),(^3),(^4),(2^)] [10]
[100,1000,1024,10000]

>apply [(*5)] (apply [(‘div‘5)] [1..100])
[0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]

解决方案

apply :: [a -> b] -> [a] -> [b]

First of all, this signature matches that of the standard <*> function, which is part of the Applicative class.

class Applicative f where
    pure :: a -> f a
    (<*>) :: f (a -> b) -> f a -> f b

Setting f ~ [] we have <*> :: [a -> b] -> [a] -> [b].

There are at least two sensible ways of writing an Applicative instance for lists. The first one takes the Cartesian product of its inputs, pairing every function with every value. If <*>'s input lists have length N and M, the output list will have length N*M. pure for this specification would put an element in a singleton list, so that pure id <*> xs = xs.

instance Applicative [] where
    pure x = [x]
    (f:fs) <*> xs = map f xs ++ (fs <*> xs)

This is equivalent to the standard Applicative instance for [].

The other sensible way of implementing Applicative zips the two lists together by applying functions to elements pointwise. If <*>'s input lists have length N and M, the output list will have length min(N, M). pure creates an infinite list, so once again pure id <*> xs = xs.

instance Applicative [] where
    pure x = let xs = x:xs in xs
    [] <*> _ = []
    _ <*> [] = []
    (f:fs) <*> (x:xs) = f x : (fs <*> xs)

This instance is available in base under the ZipList newtype.

这篇关于高阶函数用于输入函数列表和元素列表,并将函数应用于元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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