haskell-fun | A short scripts and programs | Script Programming library
kandi X-RAY | haskell-fun Summary
kandi X-RAY | haskell-fun Summary
A short scripts and programs
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of haskell-fun
haskell-fun Key Features
haskell-fun Examples and Code Snippets
Community Discussions
Trending Discussions on haskell-fun
QUESTION
Is there any general functor (not limited to endofunctor) usage in programming?
I understand the reason endofunctor employed is to make the structure simple like monoid or monad.
I also understand ultimately, all the value is settled down to a category of a programming language (such as Hask), but what I'm talking about here is endofunctor between the same category of Strings, Numbers, Booleans, or Functions.
Related questions:
...ANSWER
Answered 2020-Aug-25 at 12:25First, yes.
For example, we all know that a monoid can be defined to be a single-object category with
- arrows to be elements
- the single object has no meaning
- composition to be operator (
(<>)
in Haskell) - id arrow to be the identity (
mempty
in Haskell).
And a homomorphism between two monoids becomes a functor between two categories in this sense.
Now, say, type A
and B
are both monoids; A functor between them is just a homomorphic function f :: A -> B
that maps each A
to B
, preserving the composition.
But, wait,
f :: A -> B
is not even aFunctor
(note that I use the monospaced typeface here)!
No, it is not a Functor
in Haskell, but it still is a functor in mathematical sense.
So, to emphasize, I state it again: "Non-endo" functors ARE used in programming, and probably even more often than endofunctors.
The point here is that category theory is a highly abstract theory - It provides notions for abstracting concrete objects. We can define these notions to mean different things in different contexts.
And Hask (or Set, or subcategories of Set) is just one of these infinite definitions, which makes
- arrows to be functions
- objects to be types (or, sets)
- composition to be function composition
(.)
- id arrow to be the
id
function.
Compare this "categorical universe" definition with the "categorical monoid" definition above - congrats, you've known two different takes on categories now!
To conclude, remember that category theory itself is just some abstractions. Abstractions themselves have no meaning and no use at all. We connect them to real things, and only in this way they can bring us convenience. Understand abstract concepts through concrete examples, but NEVER simplify these concepts themselves to anything concrete (Like, never simplify functors to merely the functors between "categorical universes" (e.g. Hask, Set, etc)!).
P.S. If you ask "Is there a functor that sends Hask to another category in Haskell?" then the answer can be yes or no. For example, you can define a category Hask * Hask to contain any two types' cartesian product, and a functor
data Diag a = Diag a a
,fmap f x = Diag (f x) (f x)
that sends each typeA
to its squareA * A
. However, Hask * Hask is still a subcategory of Hask, so we may say this is an endofunctor too.
QUESTION
While studying polyvariadic functions in Haskell I stumbled across the following SO questions:
How to create a polyvariadic haskell function?
Haskell, polyvariadic function and type inference
and thought I will give it a try by implementing a function which takes a variable number of strings and concatenates/merges them into a single string:
...ANSWER
Answered 2020-May-31 at 12:17printf
works without type annotation because of type defaulting in GHCi. The same mechanism that allows you to eval show $ 1 + 2
without specifying concrete types.
GHCi tries to evaluate expressions of type IO a
, so you just need to add appropriate instance for MergeStrings
:
QUESTION
In my quest to understand and harness GHC automatic memoization, I've hit a wall: when pure functions are called with fixed values like fib 42
, they are sometimes fast and sometimes slow when called again. It varies if they're called plainly like fib 42
or implicitly through some math, e.g. (\x -> fib (x - 1)) 43
. The cases have no seeming rhyme or reason, so I'll present them with the intention of asking what the logic is behind the behavior.
Consider a slow Fibonacci implementation, which makes it obvious when the memoization is working:
...ANSWER
Answered 2019-Jul-12 at 17:22To elaborate in case it wasn't clear from @amalloy's answer, the problem is that you're conflating two things here -- the implicit memoization-like-behavior (what people mean when they talk about Haskell's "automatic memoization", though it is not true memoization!) that results directly from thunk-based lazy evaluation, and a compiler optimization technique that's basically a form of common subexpression elimination. The former is predictable, more or less; the latter is at the whim of the compiler.
Recall that real memoization is a property of the implementation of a function: the function "remembers" results calculated for certain combinations of arguments, and may reuse those results instead of recalculating them from scratch when called multiple times with the same arguments. When GHC generates code for functions, it does not automatically generate code to perform this kind of memoization.
Instead, the GHC code generates to implement function application is unusual. Instead of actually applying the function to arguments to generate the final result as a value, a "result" is immediately constructed in the form of a thunk, which you can view as a suspended function call or a "promise" to deliver a value at a later time.
When, at some future point, the actual value is needed, the thunk is forced (which actually causes the original function call to take place), and the thunk is updated with the value. If that same value is needed again later, the value is already available, so the thunk doesn't need to be forced a second time. This is the "automatic memoization". Note that it takes place at the "result" level rather than the "function" level -- the result of a function application remembers its value; a function does not remember the results it previously produced.
Now, normally the concept of the result of a function application remembering its value would be ridiculous. In strict languages, we don't worry that after x = sqrt(10)
, reusing x
will cause multiple sqrt
calls because x
hasn't "memoized" its own value. That is, in strict languages, all function application results are "automatically memoized" in the same sense they are in Haskell.
The difference is lazy evaluation, which allows us to write something like:
QUESTION
We are writing a python program that attempts to synthesize a (simple) haskell-function given input-output pairs. Throughout the run of the program, we generate haskell code and check its correctness against the user-supplied examples.
Suppose we get as input "1 2" and expected output "3". We would (eventually)
come up with the plus function. We would then run
(\x y -> x + y) 1 2
in haskell and check if it evaluates to 3.
The way we currently do things is by running the following python code:
...ANSWER
Answered 2018-Apr-13 at 10:14This seems like an odd thing to be doing.. but interesting none the less.
Two things come immediately to mind here. First is to use ghci repl instead of spawning a new process for every eval attempt. The idea is to stream your I/O into the ghci process instead of spawning a new ghc process for each attempt. The overhead of starting a new process for every eval seems to be quite performance killer. I'd usually go for expect
, but since you want python, I'll call on pexpect
:
QUESTION
I'm trying to create a polyvariadic function in Haskell, I used this answer to create a basic function. Here is the function's code :
...ANSWER
Answered 2017-Sep-06 at 21:34The simplest version only works for Integer
results.
This works off what you already wrote, taking advantage of the fact that 0
is the identity for addition.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install haskell-fun
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page