簡體   English   中英

將列表中的元素轉換為haskell中的int?

[英]convert element of list to int in haskell?

我正在嘗試制作一個函數,並且我需要指定如果列表只有1個元素會發生什么。 我希望函數返回元素,但是我不知道如何將元素轉換為Int 有什么幫助嗎? 我已經嘗試過roundfromintegerrealtofrac ,但是沒有任何效果。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM