English 中文(简体)
1. 原始数据类型和检索特征/定义
原标题:Haskell abstract datatypes and retrieving characters/defining as strings
  • 时间:2011-11-07 22:14:08
  •  标签:
  • haskell

我有数据类型,例如:

data MyData = Something1 String

and then I had a function

myFunction :: MyData -> String
myFunction x = x

职能 我想提及[S , o , m , e ......] 页: 1 然而,我有以下错误:

Couldn t match expected type [Char] with actual typeMyData Expected type: String Actual type: MyData

就我所知,[《宪章》]与“努力”相同,我已宣布第1节为“努力”,因此它应当发挥作用?

最佳回答

<代码>[Char]与<编码>String相同,但与MyData不相同。 为了获取贵国数据类别中储存的座标,你需要使用配对方式:

myFunction :: MyData -> String
myFunction (Something1 xs) = xs

这是因为<代码>数据。 关键词使数据类型完全新。 如果你只想到别的,你也可以使用<条形码><>>。 关键词:

type MyData = String

myFunction :: MyData -> String
myFunction x = x
问题回答

MyData是not,与Sting一样。 这只是非常相似的。

你可以宣布这样一类同义词:

type MyData = String

之后,MyData和Sting是同一类别的两个名字。 事实上,Sting已经是[哈尔]的一类同义词。 在这种情况下,<密码>myFunction只是身份功能代码<>id。

或者,你可以采用配对模式,从MyData公司中提取力量:

myFunction :: MyData -> String
myFunction (Something1 xs) = xs

或者,你可以利用记录中的辛迪加自动使查阅者:

data MyData = Something1 { myFunction :: String}

(如上所述,这实际上与宣布<条码> 字功能<>/条码”相同,但现在可以建造<条码>。 MyData s using the syntax Something1{ myFunction = x } as well as Something1 x





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签