将字符串转换为整数/浮点在Haskell? [英] Convert String to Integer/Float in Haskell?

查看:146
本文介绍了将字符串转换为整数/浮点在Haskell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 数据GroceryItem = CartItem ItemName价格数量| StockItem项目名称价格数量

makeGroceryItem :: String - >浮动 - > Int - > GroceryItem
makeGroceryItem name price quantity = CartItem name price quantity

我想创建一个GroceryItem使用字符串或[String]

  createGroceryItem :: [String]  - > GroceryItem 
createGroceryItem(a:b:c)= makeGroceryItem abc

我用haskell中的单词函数分解了格式[Apple,15.00,5]。我得到这个错误,我认为是因为makeGroceryItem接受一个浮点数和一个整数。但是,我如何分别做b和c Float和Int?

  *在应用程序中输入错误
表达式:makeGroceryItem a读取b读取c
** *术语:makeGroceryItem
***类型:字符串 - >浮动 - > Int - > GroceryItem
***不符:a - > b - > c - > d - > e - > f *


解决方案

code>可以将字符串解析为float和int:

  Prelude> :set + t 
Prelude>阅读123.456:: Float
123.456
it :: Float
Prelude>阅读123456:: Int
123456
it :: Int

但是问题(1)是在你的模式:

  createGroceryItem(a:b:c)= ... 

这里是一个(右联合)将一个元素预先添加到列表中。元素的RHS必须是一个列表。因此,给定表达式 a:b:c ,Haskell会推断出以下类型:

  a :: String 
b :: String
c :: [String]



。即, c 将被认为是一个字符串列表。很明显,它不能是 read ,或者传递给任何需要字符串的函数。



p>

  createGroceryItem [a,b,c] = ... 

如果列表必须包含3个项目,或者

  createGroceryItem(a :b:c:xs)= ... 

if≥3items are acceptable。



另外(2),表达式

  makeGroceryItem a read b read c 

会被解释为 makeGroceryItem 其中2个是 read 函数。你需要使用括号:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >


data GroceryItem = CartItem ItemName Price Quantity | StockItem ItemName Price Quantity

makeGroceryItem :: String -> Float -> Int -> GroceryItem
makeGroceryItem name price quantity = CartItem name price quantity  

I want to create a GroceryItem when using a String or [String]

createGroceryItem :: [String] -> GroceryItem
createGroceryItem (a:b:c) = makeGroceryItem a b c

The input will be in the format ["Apple","15.00","5"] which i broke up using words function in haskell. I get this error which i think is because the makeGroceryItem accepts a Float and an Int. But how do i make b and c Float and Int respectively?

*Type error in application
*** Expression     : makeGroceryItem a read b read c
*** Term           : makeGroceryItem
*** Type           : String -> Float -> Int -> GroceryItem
*** Does not match : a -> b -> c -> d -> e -> f*

解决方案

read can parse a string into float and int:

Prelude> :set +t
Prelude> read "123.456" :: Float
123.456
it :: Float
Prelude> read "123456" :: Int
123456
it :: Int

But the problem (1) is in your pattern:

createGroceryItem (a:b:c) = ...

Here : is a (right-associative) binary operator which prepends an element to a list. The RHS of an element must be a list. Therefore, given the expression a:b:c, Haskell will infer the following types:

a :: String
b :: String
c :: [String]

i.e. c will be thought as a list of strings. Obviously it can't be read or passed into any functions expecting a String.

Instead you should use

createGroceryItem [a, b, c] = ...

if the list must have exactly 3 items, or

createGroceryItem (a:b:c:xs) = ...

if ≥3 items is acceptable.

Also (2), the expression

makeGroceryItem a read b read c

will be interpreted as makeGroceryItem taking 5 arguments, 2 of which are the read function. You need to use parenthesis:

makeGroceryItem a (read b) (read c)

这篇关于将字符串转换为整数/浮点在Haskell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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