Again, we have recursion.įinally, we can construct an infinite list by filtering the positive integers using our ishappy function. The ishappy function is an alternative to using guards. 5 Haskell is another statically typed, purely functional language. The other function composes this with the sum function to make for easier reading later on. To start, youll find many of the same map(), filter(), reduce(), and similar. Here we use the map function to get a list of the squares of the digits. The first guard is there to end the recursion when all of the digits have been extracted. It is the equivalent of saying, 'the digits of n is a list consisting of the digits of n divided by 10 and ending in the remainder when n is divided by 10'. The otherwise condition is a recursive call. We keep repeating this process until there are no digits left. To remove that digit, we do integer division by 10. The rightmost digit is the remainder when n is divided by 10. Think about how we take apart the digits from our starting value, n. The last condition, otherwise catches everything not covered by one of the other guards. Let's take apart the program a function at a time, starting with the digit separating function. Here is the program running, using the take function to select the first 100 happy numbers, composition of sum and toDigits functions The higher-order library function filter selects every element from a list that satisfies a predicate. Here is the program to do this, leading up to using the filter function to select from a list of positive integers, only those numbers that are happy. Checking for a result of 4 is enough to determine that a number is not happy. For all unhappy numbers, this sequence will be reached somehow. If the sum of the squares of the digits is ever one of these numbers, the number is not happy. In this second example, the sequence returns to 4. Keep repeating the process with the result until the result is either 1 (which means that the number you started with was a happy number or you keep getting a repeated cycle of numbers. Take each digit in the number and square it, adding up the sum of all those squares.To find out if a number is a happy number, we carry out the following steps. There are a couple of extras thrown in that prove useful in Haskell programs. The following example makes use of all of many of the language features described on the pages up to this one. The difference between this and the map function is that this must take a predicate, an expression or function that evaluates to true or false. Haskell search1 :: (n -> Bool) -> (n -> n) -> n -> n search1 isGood childrenOf root s root where s nodes do n <- nodes filter. The filter function is a higher-order function that processes a data structure, typically a list, in some order to produce a new data structure containing exactly those elements of the original data structure that match a given condition. highers filter (>pivot) others- filter (condition here) an array will return all elements in the array satisfied the condition (still an array format).
0 Comments
Leave a Reply. |