haskell

Haskell Is there a function for creating every variation of applying a function to a list

与世无争的帅哥 提交于 2021-02-16 18:54:05
问题 I want to create a list of variations of applying a function to every element of a list. Here is a quick example of what I mean. applyVar f [a, b, c] >> [[(f a), b, c], [a, (f b), c], [a, b, (f c)]] Essentially It applies a function to each element of a list individually and stores each possible application in an array. I'm not too sure how to approach a problem like this without using indexes as I have heard they are not very efficient. This is assuming that the function f returns the same

'Repeat' in Haskell?

白昼怎懂夜的黑 提交于 2021-02-16 15:49:45
问题 I'm very new to Haskell, and I have a simple question. What function can I use with a and b that will result in a, b times. Example: a = 4 | b = 3 Would return: [4, 4, 4] Thanks! 回答1: replicate : replicate 3 4 will be: [4,4,4] When you know what's the type of the function you need (in this case it was quite obvious that the function you needed had a type similar to Int -> a -> [a] ) you can use Hoogle in order to find it. 回答2: You could also use recursion (although the solutions above should

Put adjacent elements in List into Tuples

会有一股神秘感。 提交于 2021-02-16 15:23:16
问题 Given a List of elements: xs = [a, b, c, d, ... z] where a, b, c etc are placeholders for arbitrary values. I want to implement a function adjacents :: [a] -> [(a, a)] that produces adjacentValues = [(a, b), (b, c), (c, d), ... (y, z)] In Haskell, a recursive definition is reasonably concise: adjacents :: [a] -> [(a, a)] adjacents (x:xs) = (x, head xs) : adjacents xs adjacents [] = [] Purescript is a little more verbose: adjacents :: forall a. List a -> List (Tuple a a) adjacents list = case

Implementation of function “member” using “foldr” in Haskell

喜你入骨 提交于 2021-02-16 14:02:44
问题 I tried like that: member e [] = False member e xs = foldr (==) e xs and then: member 3 [1,2,3,4,5] and I get this error message: No instance for (Num Bool) arising from the literal `3' In the first argument of `memb', namely `3' I don't know what it means... can someone help me? PS: member is a function that, given a element and a list of elements, returns whether the element belongs to that list or not. The usual implementation is: member a [] = False member a (x:xs) | (a == x) = True |

Haskell not lazy evaluating interleaving

元气小坏坏 提交于 2021-02-16 13:29:05
问题 I am working through a problem (its from a UPenn class, but I am not taking it (just working through it to learn Haskell)), and the point is to construct a Stream (as defined below), that is defined by "ruler" (ruler !! n = the exponent of the highest power of 2 which will divide n). The issue is that I think that the definition for ruler below should lazily evaluate, but it seems to be evaluating strictly and running infinitely. If I cap it by adding a terminal case like "nthStream 10 =

用欧拉计划学习Rust编程(第13~16题)

为君一笑 提交于 2021-02-13 15:22:19
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识。学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法。 学习任何一项技能最怕没有反馈,尤其是学英语、学编程的时候,一定要“用”,学习编程时有一个非常有用的网站,它就是“欧拉计划”,网址: https://projecteuler.net 这个网站提供了几百道由易到难的数学问题,你可以用任何办法去解决它,当然主要还得靠编程,编程语言不限,论坛里已经有Java、C#、Python、Lisp、Haskell等各种解法,当然如果你直接用google搜索答案就没任何乐趣了。 学习Rust最好先把基本的语法和特性看过一遍,然后就可以动手解题了,解题的过程就是学习、试错、再学习、掌握和巩固的过程,学习进度会大大加快。 第1~6题 第7~12题 第13题 大整数求和 问题描述: 有100个长达50位的大整数,求和,只取前10位数字。 各种编程语言都有大整数的函数库,直接使用就行了,不用自己造轮子。在Rust里一样也有大量的现成的库,称为crate,这个单词翻译为“柳条箱”,不知道官方的翻译是什么。大整数的官方实现是num_bigint。 需要修改Cargo.toml文件: [dependencies] num-bigint = "0.2.2" 文件头加上相关的引用: extern

Is there such a thing as maximumWith?

故事扮演 提交于 2021-02-11 12:50:32
问题 Specifically I'm searching for a function 'maximumWith', maximumWith :: (Foldable f, Ord b) => (a -> b) -> f a -> a Which behaves in the following way: maximumWith length [[1, 2], [0, 1, 3]] == [0, 1, 3] maximumWith null [[(+), (*)], []] == [] maximumWith (const True) x == head x My use case is picking the longest word in a list. For this I'd like something akin to maximumWith length . I'd thought such a thing existed, since sortWith etc. exist. 回答1: Let me collect all the notes in the

Lift instance of class with a `MonadIO` type-variable to the transformed monad

ⅰ亾dé卋堺 提交于 2021-02-11 06:37:12
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Lift instance of class with a `MonadIO` type-variable to the transformed monad

雨燕双飞 提交于 2021-02-11 06:36:49
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

haskell can not construct infinite type

坚强是说给别人听的谎言 提交于 2021-02-11 04:26:45
问题 I'm new to haskell. I wrote a simple code. But it does not work. I'm getting this 'can not construct infinite type' error. How does it fix. reverse' list | null list = [] | otherwise = (reverse' (tail list)) : (head list) 回答1: The problem arises from your use of the : operator, which has the type (:) :: a -> [a] -> [a] So it takes an element and a list, and returns a new list with that element prepended on. Where you have reverse' (tail list) : head list -- parentheses removed since they're