[英]convert element of list to int in haskell?
我正在嘗試制作一個函數,並且我需要指定如果列表只有1個元素會發生什么。 我希望函數返回元素,但是我不知道如何將元素轉換為Int
。 有什么幫助嗎? 我已經嘗試過round
, frominteger
, realtofrac
,但是沒有任何效果。
pMaiore :: Ord a => [a] -> Int
pMaiore [x]= --give x but converted to Int
pMaiore (x:xs:xss)= if x>=xs then pMaiore(x:xss)
else pMaiore(x:xss) --when x<xs
這個怎么樣?
pMaiore :: (Ord a, Integral a) => [a] -> Int
pMaiore [x]= fromIntegral x --give x but converted to Int
pMaiore (x:xs:xss)= if x>=xs then pMaiore(x:xss)
else pMaiore(x:xss) --when x<xs
如果要將a
轉換為Int
,則必須在它們之間建立一些連接。 一種方法是使用類型類。 如果您知道a
將成為浮點,則可以改用RealFrac
,然后使用round
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.