Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Relearn You a Haskell (Part 1: The Basics)
Andrew (he/him)
Andrew (he/him)

Posted on

     

Relearn You a Haskell (Part 1: The Basics)

I worked my way throughLearn You a Haskell for Great Good! about six months ago and I haven't worked much with Haskell since then. So of course I forgot everything. So I've worked back through it again and made this handy refresher guide (mostly for myself but also) for you to have a quick reminder how things work in Haskell-land:

not

Haskell doesn't use~ or! to denote negation, rather, it uses thenot function:

ghci>notTrue-- comments come after two hyphensFalse
Enter fullscreen modeExit fullscreen mode

...but it does use/= to mean "is not equal to":

ghci>5/=4True
Enter fullscreen modeExit fullscreen mode

function application

Haskell doesn't use parentheses to separate a function's name from its arguments; rather, we just use spaces:

ghci>max344
Enter fullscreen modeExit fullscreen mode

partial function application

Partially-applied functions can be saved in variables and used later!

ghci>min5=min5ghci>min544ghci>min565
Enter fullscreen modeExit fullscreen mode

infix notation

Some functions make more sense when they appear between their two parameters, rather than before them. For instance:

ghci>div1005-- prefix notation20ghci>100`div`5-- infix notation20
Enter fullscreen modeExit fullscreen mode

functions

Define basic functions by putting the named parameters before the= and the function definition after:

ghci>pythagorasab=sqrt(a*a+b*b)ghci>pythagoras51213.0
Enter fullscreen modeExit fullscreen mode

Note that function names can contain' (usually used to denote strict versions of otherwise lazy functions) and cannot begin with capital letters.

lists

In Haskell, lists are homogeneous and strings are lists of characters:

ghci>ll=[1,2,3,4]ghci>ss=['e','l','l','o']
Enter fullscreen modeExit fullscreen mode

A single element can be prepended (fast) to a list with: and a list can be appended (slow) to a list with++:

ghci>'h':ss"hello"ghci>ll++[5]++[6,7][1,2,3,4,5,6,7]ghci>'s':'m':ss-- : and ++ can be chained like this"smello"
Enter fullscreen modeExit fullscreen mode

Elements can be extracted with the!! operator (unlike the[] operator in C):

ghci>ll!!2-- equivalent to ll[2] in C3
Enter fullscreen modeExit fullscreen mode

A list can itself contain lists of items or lists of lists of items and all of those lists can be different lengths, as long as they all contain the same type of objects (numbers or characters, but not both).

Finally, lists are compared in lexicographical order, meaning that the first elements are compared first, then the second elements, etc:

ghci>[1,2,3]>[0,4,5]Trueghci>[1,2,3]>[1,2]True
Enter fullscreen modeExit fullscreen mode

list functions

head /tail /init /last /length

ghci>ll[1,2,3,4]ghci>headll-- first element of list1ghci>tailll-- all but first element of list[2,3,4]ghci>initll-- all but last element of list[1,2,3]ghci>lastll-- last element of list4ghci>lengthll-- length of list4
Enter fullscreen modeExit fullscreen mode

Note thathead,tail,init andlast all throw errors if they're called on an empty list, butlength returns 0. You can check if a list is empty withnull:

ghci>length[]-- length of an empty list is zero0ghci>lengthll-- ll has 4 elements in it at indices 0..34ghci>null[]-- an empty list is nullTrueghci>nullllFalse
Enter fullscreen modeExit fullscreen mode

reverse

ghci>reversell-- reverse a list[4,3,2,1]
Enter fullscreen modeExit fullscreen mode

take /drop

ghci>take1ll-- take 1 element from the beginning of the list[1]ghci>take3ll-- take 3 elements[1,2,3]ghci>drop1ll-- drop 1 element from the beginning of the list[2,3,4]ghci>drop3ll-- drop 3 elements[4]
Enter fullscreen modeExit fullscreen mode

maximum /minimum

ghci>maximumll4ghci>minimumll1
Enter fullscreen modeExit fullscreen mode

sum /product

ghci>sumll-- 1 + 2 + 3 + 410ghci>productll-- 1 * 2 * 3 * 424
Enter fullscreen modeExit fullscreen mode

elem

elem works like sort of like howcontains() works in other languages, it returns true if the first argument is an element of the second argument, which must be a list:

ghci>elem2llTrueghci>5`elem`llFalse
Enter fullscreen modeExit fullscreen mode

cycle /repeat /replicate

Repeat a list withcycle; repeat a single object withrepeat

ghci>take10(cyclell)[1,2,3,4,1,2,3,4,1,2]ghci>take10(repeat5)[5,5,5,5,5,5,5,5,5,5]ghci>take5(repeatll)[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]ghci>replicate105-- same as take 10 (repeat 5)[5,5,5,5,5,5,5,5,5,5]
Enter fullscreen modeExit fullscreen mode

ranges

Ranges work with integers and characters:

ghci>[1..10][1,2,3,4,5,6,7,8,9,10]ghci>['a'..'j']"abcdefghij"
Enter fullscreen modeExit fullscreen mode

The first two elements of a list can be given to define a pattern:

ghci>['z','w'..'a']"zwtqnkheb"ghci>[26,23..1][26,23,20,17,14,11,8,5,2]
Enter fullscreen modeExit fullscreen mode

Above, the list ends when the last element is hit, but you can also just define how many elements you want by usingtake:

ghci>take10[1,3..][1,3,5,7,9,11,13,15,17,19]
Enter fullscreen modeExit fullscreen mode

Coming up in Part 2: list comprehensions, tuples, and typeclasses

Top comments(19)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

Great post, I really want to get into Haskell

CollapseExpand
 
antonrich profile image
Anton
Born into being.
  • Location
    Russia, Yoshkar-Ola
  • Education
    The self
  • Joined

Have you triedTry Haskell?

CollapseExpand
 
chenge profile image
chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
  • Location
    China
  • Joined

This is great for new. I feel Haskell is more easy to remember after you understand the type syntax like :: => ->. This will help you write program more fluently.

A speaker told two break points: monad and category.

CollapseExpand
 
awwsmm profile image
Andrew (he/him)
Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français.
  • Email
  • Location
    Ottawa, Canada
  • Education
    Ph.D. in [Astroparticle] Physics
  • Pronouns
    he / him
  • Work
    Principal Consultant at Improving
  • Joined

I missed this comment and just checked this out today... it's great! I'll have to keep this link for reference.

CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

Nope. This is great!

CollapseExpand
 
antonrich profile image
Anton
Born into being.
  • Location
    Russia, Yoshkar-Ola
  • Education
    The self
  • Joined

I really like Haskell's repl because it allows you to learn types quickly.

You just type:

:t the_name_of_the_function.

:t - stands for :type.

CollapseExpand
 
jvanbruegge profile image
Jan van Brügge
Student of the technical university munich, chief software engineer for the MOVE-II CubeSat, likes functional programming and sports
  • Joined

Just a minor nitpick,not is not a keyword, but a function

CollapseExpand
 
awwsmm profile image
Andrew (he/him)
Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français.
  • Email
  • Location
    Ottawa, Canada
  • Education
    Ph.D. in [Astroparticle] Physics
  • Pronouns
    he / him
  • Work
    Principal Consultant at Improving
  • Joined

Fixed!

CollapseExpand
 
antonrich profile image
Anton
Born into being.
  • Location
    Russia, Yoshkar-Ola
  • Education
    The self
  • Joined

not True -- comments come after two hyphens

instead of the bang operator (!==) it uses (/=) operator for not True in comparison.

CollapseExpand
 
awwsmm profile image
Andrew (he/him)
Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français.
  • Email
  • Location
    Ottawa, Canada
  • Education
    Ph.D. in [Astroparticle] Physics
  • Pronouns
    he / him
  • Work
    Principal Consultant at Improving
  • Joined

Right! That's another idiosyncrasy of Haskell that I neglected to mention.

CollapseExpand
 
joshcheek profile image
Josh Cheek
  • Joined

I'm fine with it. SQL's not-equal-to operator is<> things like that really don't matter. It's like being upset about single quotes vs double quotes. If you think that's annoying, then you should absolutely learn Haskell, because you're focused on aesthetics instead of big picture implications, and Haskell will force you to learn better ways of thinking about the big picture.

Most of the problems that most of us have are simply difficult and frowned upon in Haskell.

Thread Thread
 
awwsmm profile image
Andrew (he/him)
Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français.
  • Email
  • Location
    Ottawa, Canada
  • Education
    Ph.D. in [Astroparticle] Physics
  • Pronouns
    he / him
  • Work
    Principal Consultant at Improving
  • Joined

I think maybe you misunderstood what one of us said. All we did was note that the "not equals" operator is different than the "normal" one in Haskell. Which is a good thing to know whether you're focused on aesthetics or not.

Thread Thread
 
drbearhands profile image
DrBearhands
  • Education
    MSc. Artificial Intelligence
  • Joined

While I get what you're saying, thenormal not-equal symbol is. So calling it an idiosyncrasy sounds like c-style bias, as Haskell's version is closer to the mathematical symbol.

CollapseExpand
 
dwayne profile image
Dwayne Crooks
Learn to build reliable web applications with Elm.
  • Location
    Trinidad & Tobago
  • Joined

LYAH is a nice introduction to Haskell but if you're serious about understanding the language I'd recommend givingHaskell Programming from First Principles a read.

Just curious. Is there any particular application that you'd like to build in Haskell?

CollapseExpand
 
deciduously profile image
Ben Lovy
Just this guy, you know?
  • Email
  • Location
    Boston, MA, USA
  • Education
    Currently enrolled @ Champlain College Online
  • Work
    Rust Developer @ Tangram.dev
  • Joined

Nice and concise! I'd completely forgotten aboutcycle

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

Thank you so much for this,@joshcheek writes Haskell for some coding puzzles and I really want to learn it as a result!

CollapseExpand
 
chenge profile image
chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
  • Location
    China
  • Joined

Free Great Book:Learn You a Haskell

CollapseExpand
 
chenge profile image
chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
  • Location
    China
  • Joined
• Edited on• Edited

Good post, and another intro video.

CollapseExpand
 
moopet profile image
Ben Sinclair
I've been a professional C, Perl, PHP and Python developer.I'm an ex-sysadmin from the late 20th century.These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
  • Location
    Scotland
  • Education
    Something something cybernetics
  • Pronouns
    They/them
  • Work
    General-purpose software person
  • Joined

I worked my way throughLearn you about six months ago and have also forgotten most of it!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français.
  • Location
    Ottawa, Canada
  • Education
    Ph.D. in [Astroparticle] Physics
  • Pronouns
    he / him
  • Work
    Principal Consultant at Improving
  • Joined

More fromAndrew (he/him)

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp