haskell

An ArrowCircuit instance for stream processors which could block

半世苍凉 提交于 2021-01-21 07:30:30
问题 The Control.Arrow.Operations.ArrowCircuit class is for: An arrow type that can be used to interpret synchronous circuits. I want to know what synchronous means here. I looked it up on Wikipedia, where they are speaking of digital electronics. My electronics is quite rusty, so here is the question: what is wrong (if anything is) with such an instance for the so-called asynchronous stream processors: data StreamProcessor a b = Get (a -> StreamProcessor a b) | Put b (StreamProcessor a b) | Halt

Haskell - alternating elements from two lists

家住魔仙堡 提交于 2021-01-20 19:12:55
问题 I'm trying to write a haskell function that takes in two lists of integers and generates a list with elements that have been taken alternatingly from the two lists. I have the function: blend xs ys An example: blend [1,2,3] [4,5,6] should return [1,4,2,5,3,6] My logic is to zip the two lists together, generating the pairs of alternate elements, then somehow remove them from their tuples. It's removing them from their tuples that I can't figure out how to implement. 回答1: How about exchanging

Inlining derived typeclass methods

独自空忆成欢 提交于 2021-01-20 18:58:19
问题 Haskell lets you derive typeclass instances, such as: {-# LANGUAGE DeriveFunctor #-} data Foo a = MakeFoo a a deriving (Functor) ... but sometimes benchmarks show that performance improves if you manually implement the typeclass instance and annotate the type class method(s) with INLINE , like this: data Foo a = MakeFoo a a instance Functor Foo where fmap f (MakeFoo x y) = MakeFoo (f x) (f y) {-# INLINE fmap #-} Is there a way to get the best of both worlds? In other words, is there a way to

Inlining derived typeclass methods

隐身守侯 提交于 2021-01-20 18:56:37
问题 Haskell lets you derive typeclass instances, such as: {-# LANGUAGE DeriveFunctor #-} data Foo a = MakeFoo a a deriving (Functor) ... but sometimes benchmarks show that performance improves if you manually implement the typeclass instance and annotate the type class method(s) with INLINE , like this: data Foo a = MakeFoo a a instance Functor Foo where fmap f (MakeFoo x y) = MakeFoo (f x) (f y) {-# INLINE fmap #-} Is there a way to get the best of both worlds? In other words, is there a way to

How to print the same char n times in Haskell

▼魔方 西西 提交于 2021-01-20 08:12:09
问题 I would like to print a char underlining a String n times, with n the length of the String in Haskell. How should I do this? My String is: "Available Chars (x)" and I want to have a char underlining that sentence, which should have exactly the same length as the "Available Chars (x)". But x is an int, so it could be "1" or "10" or "1000" etc.. so the length is variable. I got the length but i didnt know how to print that char as long as the string is... 回答1: Use replicate: underline :: String

How to print the same char n times in Haskell

非 Y 不嫁゛ 提交于 2021-01-20 08:11:31
问题 I would like to print a char underlining a String n times, with n the length of the String in Haskell. How should I do this? My String is: "Available Chars (x)" and I want to have a char underlining that sentence, which should have exactly the same length as the "Available Chars (x)". But x is an int, so it could be "1" or "10" or "1000" etc.. so the length is variable. I got the length but i didnt know how to print that char as long as the string is... 回答1: Use replicate: underline :: String

Ambiguous type variable in a test for empty list

旧时模样 提交于 2021-01-20 07:09:14
问题 Consider the following snippet which defines a function foo which takes in a list and performs some operation on the list (like sorting). I tried to load the snippet in ghci : -- a function which consumes lists and produces lists foo :: Ord a => [a] -> [a] foo [] = [] foo (x:xs) = xs test1 = foo [1, 2, 3] == [2, 3] test2 = null $ foo [] yet the following error occurs: No instance for (Ord a0) arising from a use of ‘foo’ The type variable ‘a0’ is ambiguous Note: there are several potential

Pick a random element from a list in Haskell

丶灬走出姿态 提交于 2021-01-20 07:09:06
问题 My code aims to create a word search puzzle. There is a data called Orientation representing the direction of each word in the puzzle. data Orientation = Forward | Back | Up | Down | UpForward | UpBack | DownForward | DownBack deriving (Eq, Ord, Show, Read) Now given a input of strings which is [String] , I want to randomly assign each string an orientation like [(Orientation, String)] assignWordDir :: [String] -> [(Orientation, String)] assignWordDir [] = [] assignWordDir (s:strs) = (ori, s)

Ambiguous type variable in a test for empty list

落花浮王杯 提交于 2021-01-20 07:07:12
问题 Consider the following snippet which defines a function foo which takes in a list and performs some operation on the list (like sorting). I tried to load the snippet in ghci : -- a function which consumes lists and produces lists foo :: Ord a => [a] -> [a] foo [] = [] foo (x:xs) = xs test1 = foo [1, 2, 3] == [2, 3] test2 = null $ foo [] yet the following error occurs: No instance for (Ord a0) arising from a use of ‘foo’ The type variable ‘a0’ is ambiguous Note: there are several potential

搭载M1的新Mac到底适不适合你

浪子不回头ぞ 提交于 2021-01-07 19:26:45
// 每日前端夜话 第465篇 // 正文共:5300 字 // 预计阅读时间:10 分钟 每次 Apple 的新品发布活动都吸引了很多目光,但是 2020 MacBook Air,MacBook Pro 和 Mac Mini 的发布不仅仅是新功能和时尚的工业设计。重大新闻隐藏在了新 MacBook 的阳极氧化铝机箱中的 M1 片上系统(SoC)中,它取代了 13 寸 MacBook 和 Mac Mini 中的英特尔处理器。 为什么过渡到 Apple 处理器这么重要?毕竟苹果自 2010 年以来就一直在设计它的智能手机处理器,这并不是它第一次采用新的 CPU 架构。苹果已经经历了许多不同的体系结构,从 Wozniak 时代的摩托罗拉 CPU 到 90 年代的PowerPC 芯片,再到 2005 年的英特尔 x86 处理器。现在他们正朝着自己设计的基于 ARM 的处理器发展。相比之下,自 1981 年 IBM 推出第一台 PC以来,Windows PC 平台就一直停留在 x86 处理器上。 今天,我们将仔细研究 Apple 的新型 M1 芯片,以及它对软件工程师、设计师、消费者和整个行业的影响。为了简洁起见,我不会为你提供详细的性能评估,也不会测试每个软件和工具、旧版软件、SDK 等的兼容性,因为你可以轻松的找到自己信任测试和评估数据,我们把重点放在全局上,并解决有兴趣购买新款 M1