函数获取一个Int并返回一个列表 [英] function that gets an Int and returns a list
问题描述
<$ c在Haskell中我需要创建各种类型的练习。第一个类型称为Finite,定义如下: $ c> type Finite a = [a]
然后我需要返回一个已定义的单例像这样
singleF :: a - >有限的
所以我实现了它:
单身n = [n]
创建另一个类型
type Enumeration a = Int - >有限的
然后我需要重新实现singleton函数
singleE :: a - >枚举a
在我的理解类型 Enumeration
是一个从Int到<
通过练习(前一种类型有限也被称为桶):枚举是无限序列的有限桶,由自然数索引。 code>。
而函数single:为简单起见,我建议您将唯一的项目放在存储区0
中, m认为int是枚举中桶的索引
关闭我的头顶:
singleE :: a - >枚举a
singleE a 0 = singleF a
singleE _ _ = []
$ b $ main :: IO()
main = do
let s = singleE'a'
print $ s 0
print $ s 5
给出
a
singleE给你一个函数,它接受一个Int并返回一个Finite。如果你传递0,你会得到一个单一元素的Finite,否则是空的。
I have an exercise in Haskell where I need to create various types.The first type is called Finite which is defined like this:
type Finite a = [a]
and then I need to return a singleton which is defined like this
singleF :: a -> Finite a
so I implemented it like so:
single n = [n]
Then later I create another type
type Enumeration a = Int -> Finite a
then I need to reimplement the singleton function
singleE :: a -> Enumeration a
In my understanding the type Enumeration
is a synonym for a function from an Int to a list of type a
, but I can't understand how exactly I can implement that.
From the exercise (the previous type 'Finite' is also referred to as a 'bucket'): An enumeration is an infinite sequence of finite buckets, indexed by natural numbers
.
And the function single : I suggest for simplicity that you put the sole item in bucket 0
, So I'm thinking that the int is the index of the bucket in the enumeration
Off the top of my head:
singleE :: a -> Enumeration a
singleE a 0 = singleF a
singleE _ _ = []
main :: IO ()
main = do
let s=singleE 'a'
print $ s 0
print $ s 5
Gives
"a"
""
singleE gives you a function that takes an Int and returns a Finite. If you pass 0, you get a Finite with the single element, otherwise an empty one.
这篇关于函数获取一个Int并返回一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!