在Haskell中添加两个函数 [英] Adding two functions together in Haskell

查看:111
本文介绍了在Haskell中添加两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是Haskell的新成员,我遇到了一个有趣的问题,但我不确定如何解决它。我只是想向你展示这个问题的两个部分。



问题是我们要输入13到15位数字。
然后从那个数字我们删除最后一个数字。如19283828382133应该输出完全相同的数字,而不是最终的3,1928382838213。

然后每个奇数(而不是数字)从这些数字将加倍。所以你会得到2,9,4,8​​,6等等。



到目前为止,这是我的代码。从代码中可以看出,我可以单独完成这两个部分(工作),但我不确定我会将它们加在一起。

  lastdigit :: Integer  - >整数 - 这个函数删除最后一个数字
lastdigit x = x`div`10

doubleOdd(x:xs)=(2 * x):( doubleEven xs) - 这个函数双数每个奇数都不是数字。
doubleOdd [] = []
doubleEven(x:xs)= x:(doubleOdd xs)
doubleEven [] = []
pre>

因此,为了进一步解释我正在尝试构建的程序,首先要经过13到15位数字的步骤。然后它会首先删除最后一个数字,然后自动进入下一个步骤,将每个奇数(不是数字)加倍。谢谢

解决方案

首先,您需要一种方法将一些大数字分解为数字。

  digits :: Integral x => x  - > [x] 
digits 0 = []
digits x = digits(x`div` 10)++ [x`mod` 10]

这给你...

  Prelude>数字12345 
[1,2,3,4,5]

然后您可以放弃最后一位数字为 init

  Prelude> (init。digits)12345 
[1,2,3,4]

map 在列表中的奇数元素上。 [] = []
mapOdd f(x:[])= [fx]
mapOdd f(x:y:rest)= fx:y:mapOdd f rest



$ p $ 前奏> mapOdd(+10)[1..10]
[11,2,13,4,15,6,17,8,19,10]

还有一个函数可以返回大量数据...

  undigits = sum。 zipWith(*)[10 ^ n | n < -  [0 ..]]。反向

导致...

 前奏> undigits [1,2,3,4] 
1234

/ p>

  Prelude>头饰。 mapOdd(* 2)。在里面 。数字$ 12345 
2264






在功能特别是语言,总是试图通过解决小问题来解决问题:)

Hi I am new in Haskell and I came across an interesting problem but I was not really sure on how I would go about solving it. I am about to show you only two parts of the question as an example.

The question is that we are to input a number between 13 to 15 digits. then from that number we remove the last number. such as 19283828382133 should out put the exact same number just without the final 3, 1928382838213.

Then every odd digit(not number) from these numbers will be doubled. So you will get 2,9,4,8,6 etc

This is my code so far. As you can see from the code I have been able to complete these two parts individually(working) but I am not sure how I would add them together.

lastdigit :: Integer -> Integer -- This Function removes the last number
lastdigit x = x`div`10

doubleOdd (x:xs) = (2*x):(doubleEven xs) -- This function doubles every odd digit not number.
doubleOdd [] = []
doubleEven (x:xs) = x:(doubleOdd xs)
doubleEven [] = []

So to further explain the program I am trying to build will first go through the step of taking in the number between 13 to 15 digits. Then it will first remove the last number then automatically go to the next step of doubling each odd digit(not number). Thanks

解决方案

First, you need a way to break some large number into digits.

digits :: Integral x => x -> [x]
digits 0 = []
digits x = digits (x `div` 10) ++ [x `mod` 10]

Which gives you...

Prelude> digits 12345
[1,2,3,4,5]

You can then drop the last digit with init

Prelude> (init . digits) 12345
[1,2,3,4]

The a helper function to map over odd elements in a list.

mapOdd _ [] = []
mapOdd f (x:[]) = [f x]
mapOdd f (x:y:rest) = f x : y : mapOdd f rest

Giving you...

Prelude> mapOdd (+10) [1..10]
[11,2,13,4,15,6,17,8,19,10]

And a function to get back to a large number...

undigits = sum . zipWith (*) [10^n | n <- [0..]] . reverse

Resulting in...

Prelude> undigits [1, 2, 3, 4]
1234

And putting it all together

Prelude> undigits . mapOdd (*2) . init . digits $ 12345
2264


In functional languages particularly, always try to solve a problem by composing solutions to smaller problems :)

这篇关于在Haskell中添加两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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