Home
Insert
Update snippet 'The Haskell const function'
Title
Description
The const function is simple, but you can use it to make your code more legible. In this example we convert a unary function to a function of arity 2 (that ignores the second argument). Also by using the flip function from Haskell (which is equally easy to define) you can ignore the first argument.
Source code
// [snippet:Declarations] // The const function let ct a _ = a // The flip function (not used in this snippet) let flip f x y = f y x // [/snippet] // [snippet:Use case] // Code taken from a toy interpreter type Exp = // ... // Lambdas are expressed as F# an function that takes a list of Exps and an env | Lambda of (Exp list -> Env -> Exp) // ... and Env = (string * Exp) list // Built-ins receive evaluated arguments so they don't care about the env. // By composing the function we get with ct we're basically converting a function // of arity 1 to a function of arity 2 that just ignores its second argument let builtin f = Lambda (f >> ct) // [/snippet]
Tags
haskell functions
haskell functions
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update